• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

anonymous classes

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I can't figure out something about anonymous classes.
As far as I have understood by now, anonymous classes represent a specified kind of local (to method) inner classes.
Hence, here is what we can read in Complete Java 2 Certification Study Guide by Simon Roberts (page 205):

Some classes that you define inside a method do not need a name . A class defined in this way without a name is called an anonymous class
...
This means that anonymous inner classes are unique to method scopes; you cannot have anonymity with a member class


This is OK.
But in dan chisholm's mock exam devoted to anonymous classes, we can find the following example that compiles and runs wells (prints 5):

Can't we say here that the following code:
static A a1 = new A(2) {
{incY();}
public int math() {return x()+y();}
};

defines an anonymous class as a member class of class B? And that the restrictions that are supposed to be applied (actually not qualify them as static) to anonymous inner class do not work any more? That's why a1 is declared as static...
I think I'm wrong about something, but I can't grasp about what exactly...
I would be very grateful for any assistance,
Thanks in advance,
Cyril.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cyril,
It is the variable a1(a reference type) which is the member of class B. It just so happen that it is initialized to point to an object created thru anonymous class.

Originally posted by cyril vidal:

As far as I have understood by now, anonymous classes represent a specified kind of local (to method) inner classes.


Anonymous class doesn't have to be defined inside a method.
[ July 05, 2003: Message edited by: Alton Hernandez ]
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton,
thanks for your response.
So, the following line extracted from the book isn't correct, or, at least, ambiguous?

Some classes that you define inside a method do not need a name . A class defined in this way without a name is called an anonymous class...
This means that anonymous inner classes are unique to method scopes


And what about the assertion telling that anonymous classes can't be static?
I've understood that in the example, it was a reference to an object (so an instance and not the class by itself) but in the case of anonymous classes, can we have something else than an instance, either of this following form:
new Xxxx() {/* class body */}
or this one:
Xxxx anXxxx = new Xxxx() {/* class body */};
For me, telling of an anonymous class or telling of an instance of anonymous class is the same (this instance may be referenced or not).
Am I wrong on this point?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



So, the following line extracted from the book isn't correct, or, at least, ambiguous?


I don't have a copy of that book but I believe that the author is just talking about where you would normally use an anonymous class.


..can we have something else than an instance, either of this following form:
new Xxxx() {/* class body */}
or this one:
Xxxx anXxxx = new Xxxx() {/* class body */};
For me, telling of an anonymous class or telling of an instance of anonymous class is the same (this instance may be referenced or not).


When you 'define' an anonymous class, you start with the new statement. This tells you what makes anonymous class different from the others. It is instantiated at the location where it is defined. But the definition and instantation of the class are still 2 different thing.


And what about the assertion telling that anonymous classes can't be static?


As per JLS 8.1.2, an inner class is a nested class that is NOT explicitly or implicitly declared static, and an anonymous class is considered as an inner class.
[ July 05, 2003: Message edited by: Alton Hernandez ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried to compile such a class with a static member but the compiler complains:
"inner classes cannot have static declarations"
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic