• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

anonymous class

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
false.
Anonymous classes can be instantiated anywhere. When they are created within an enclosing method scope, they are called 'Anonymous Inner class'.
Ajith
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Woudnt "true" be a more appropriate answer?
 
Enthuware Software Support
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. But do you agree with me if I say an anonymous class is different than an anonymous inner class?
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So,
What is the final answer of the original question?
-Mugdha
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is true.
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic