• 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 Classes

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer true or false:
anonymous classes cannot be static.
The answer mentioned was true.Should'nt it be false because,anonymous classes cannot be "declared static" but,they can be considered as static/non-static if they are enclosed within a static/non-static context(say a method)respectively. For instance,anonymous classes declared within a static context can only access "static members" in the enclosing context.But,this restriction does not apply to anonymous classes declared within a non-static context.
Pls let me know if i am wrong .
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if the inner class is defined in a static method, that doesn't mean the class is static. The best thing for you to do would be:
Just write a small program, and in it's main() method create an anonymous class. Now after compiling it, you will see there are 2 .class files: (Assume the name of you class is TestClass.java)
TestClass.class and TestClass$1.class
Now, run javap TestClass$1
You see that this class is not static. Play with it a little bit and you'll understand. Also define a static class and run the same test.
Another point, you should be able to refer to a static class by it's name. And anonymous class doesn't have one. You always do 'new' to use an anonymous class.
(BTW, anonymous class do have a name but that name is not known to the programmer. In the above example, the name of the anonymous class is TestClass$1 !)
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited December 19, 2000).]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way to approach this question is to consider the rule for any static inner class. A static inner class is more like an "embedded" class: it's written within the enclosing braces of another class, but maintains its own "top-level" identity. A nameless top-level class would make no sense, so remembering that point might help to clarify how you think about this question.
Also, as Paul says, an instance that is only accessible within a static context doesn't mean the class itself must be static. A static class would ultimately have to be final; a static reference simply means there's no per-copy instance of the thing in memory.
-----------------
Michael Ernest, co-author of:

    The Complete Java 2 Certification Study Guide


    [This message has been edited by Michael Ernest (edited December 19, 2000).]
 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks !!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic