This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
true or false An instance of anonymous inner class can only be created in it's outer class. Can anyone give me the correct answer and an explanation, please.
false. Anonymous classes can be instantiated anywhere. When they are created within an enclosing method scope, they are called 'Anonymous Inner class'. Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Yes. Anonymous class can only be constructed within it's outer class. Anonymous class is acutally, declaration, definition and instantiation combined. You cannot even define an 'anonymous' class in package scope let alone instantiate it. And so anonymous classes are always 'inner' classes, are always non-static and are always created inside an outer class. You could also think it this way that as the declaration/definition for an anonymous class does not exist seperately from it's instantiation, there is no way you can instantiate it some place other than where you define it. And as it's definition has to be inside a class, it is not possible to use it outside it's outer class. HTH, Paul. ------------------ Get Certified, Guaranteed! (Now Revised for the new Pattern) www.enthuware.com/jqplus
Well, I beg to differ about the terminology here. The following example illustrates an anonymous class, not an anonymous inner class. I say so because this class is being instantiated in an enclosing class scope.
Whereas, the following is an example for a true anonymous inner class since it is declared in an enclosing method context.
So it is a question of , when a class is an inner class? If you read the Inner class specification document, it says
In previous releases, Java supported only top-level classes, which must be members of packages. In the 1.1 release, the Java 1.1 programmer can now define inner classes as members of other classes, locally within a block of statements, or (anonymously) within an expression
I am sure a lot of people will not agree with me
Ajith
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2922
posted
0
Ajith, According to the definition of inner class both of your examples are inner class examples. And for obvious reason both are anonymous. The second example is actually a 'local' inner class which happens to be anonymous. I do not think you can subcategorize them in 'true' or non-true inner class inner class. Even if you do they are still inner classes. The quote from JLS that you have given clearly says that all the 3 types of classes ie. members of other classes, locally within a block of statements, or (anonymously) within an expression ARE inner classes.
-Paul. ------------------ Get Certified, Guaranteed! (Now Revised for the new Pattern) www.enthuware.com/jqplus
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
Agreed. But do you agree with me if I say an anonymous class is different than an anonymous inner class?
Sahir Shah
Ranch Hand
Joined: Nov 05, 2000
Posts: 158
posted
0
I agree with Paul's statement that its a case of declaration , definition and instantiation all rolled into one. However, regarding not being able to use it outside the enclosing class I beg to differ. You can access the object outside the enclosing class. This example elucidates the use of an anonymous class object and an anonymous local class object outside the enclosing class. <pre> interface BugzBunny{ public void talk(); } interface Duck{ public void quack(); } class Disney { public static BugzBunny bugsie = new BugzBunny() { public void talk() { System.out.println("whats up doc?"); } }; Duck getDonald() { return new Duck(){ public void quack() { System.out.println("quack quack quack"); } }; } } class Test{ public static void main(String[] args) { Disney.bugsie.talk(); Disney walt = new Disney(); Duck donald = walt.getDonald(); donald.quack(); } } </pre>
Rgds Sahir [This message has been edited by Sahir Shah (edited December 21, 2000).]
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2922
posted
0
Sahir, exporting a reference of anonymous inner class object was never an issue and what you are trying to say is correct and fine. By saying "...cannot use outside...", I meant the class and not the object of the class. I wrote, "And as it's definition has to be inside a class, it is not possible to use it outside it's outer class. ". You cannot use the definition of class to create an instance outside. Ajith, I think anonymous class and anonymous inner class are not different because an annonymous class has to be inner. (I.e if a class is anonymous, it is needless to say it is inner.) However, anonymous 'method local' inner class is different than anonymous 'member' inner class. But again the two are only as different as non-anonymous 'method local' inner class and non-anonymous 'member' inner class. But I guess there is not much technicality in this as all this is just a matter of terminology -Paul.
------------------ Get Certified, Guaranteed! (Now Revised for the new Pattern) www.enthuware.com/jqplus
[This message has been edited by Paul Anil (edited December 21, 2000).]
So, What is the final answer of the original question? -Mugdha
Sahir Shah
Ranch Hand
Joined: Nov 05, 2000
Posts: 158
posted
0
Obviously, because it has no name and hence the constructor itself is anonymous how can you instantiate it anywhere other than where it is declared and defined. However if you look at it from another angle you can see that the anonymous local class's object is not instantiated until you call the enclosing method. And since the method was called from outside the enclosing class you can say that the object was remotely created from outside the enclosing class. Mughdha, There is no right or wrong here. Every thing is maya (an illusion). Buddham sharanam gatchchami , Dharmam sharanam gatchchami, Sangham sharanam gatchchami Rgds Swami Javananda Sahir [This message has been edited by Sahir Shah (edited December 21, 2000).]
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2922
posted
0
The answer is true. -Paul. ------------------ Get Certified, Guaranteed! (Now Revised for the new Pattern) www.enthuware.com/jqplus
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.