• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware question doubt

 
Hareendra Reddy
Ranch Hand
Posts: 173
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the question from here(Qno: 14)..

The answers are given as c and d ...
I believe only d is correct and how can we can know about the nature of anonymous class??

Thanks
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hareendra Reddy wrote:I got the question from here(Qno: 14)..

The answers are given as c and d ...
I believe only d is correct and how can we can know about the nature of anonymous class??

Thanks

Dear friend..... c AND d are correct! A class created INSIDE a method (including static methods) is called METHOD-LOCAL-INNER-CLASS.
The same rules apply to LOCAL VARIABLE DECLARATIONS.
The ONLY modifiers you can use are either abstract OR final. NEVER both at the same time!!.
So c is correct! as well as d.

Try this:


Remember that I am studying java just like you AND others, DO NOT rely on me 100%, I am NOT above mistakes. Thank you for your understanding.

Hey! After reading CAREFULLY the options I agree with you that option c is wrong!.
Here is my understanding:
1. class created inside the main method "IS" final.
2. class created inside the main method "CAN BE" final. // this sounds better.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ikpefua Jacob-Obinyan ...

Yeah you are saying right, anonymous inner classes are similar to method lcoal inner classes...
But we didn't mark the class final (any way we can't) , then how can we say that it is final ??
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hareendra Reddy wrote:Thank you Ikpefua Jacob-Obinyan ...

Yeah you are saying right, anonymous inner classes are similar to method lcoal inner classes...
But we didn't mark the class final (any way we can't) , then how can we say that it is final ??


It is NOT final and at the moment I have NOT seen such a syntax, I seriously doubt if it exists.

Let us analyse line 15 of your code READ CAREFULLY:



Observation:
Did you take note of the semi-colon that ends the curly braces in line 11 ??
That is how to know an anonymous inner class.
The semi colon ought to have ended the instance declaration in line 4!! but it did not!!
That is how you identify anonymous inner classes.
The SOLE purpose of anonymous inner class is to override methods.
I am sorry I dont have more details, you will have to investigate that yourself. Thanks.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3764
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
The given answer is correct.
From Java Language Specification:

15.9.5 Anonymous Class Declarations
An anonymous class declaration is automatically derived from a class instance creation expression by the compiler.
An anonymous class is never abstract (§8.1.1.1). An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.2). An anonymous class is always implicitly final (§8.1.1.2).


HTH,
Paul.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:Hello,
The given answer is correct.
From Java Language Specification:

15.9.5 Anonymous Class Declarations
An anonymous class declaration is automatically derived from a class instance creation expression by the compiler.
An anonymous class is never abstract (§8.1.1.1). An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.2). An anonymous class is always implicitly final (§8.1.1.2).


HTH,
Paul.

Thanks Paul...I was NOT sure about the issue of final in an anonimous classes and this information is FUNDAMENTAL to learners like us.
Once again thanks for your intervention.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul Aniprem...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic