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

Question about Anonymous classes

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi JavaRanch,

Im preparing for OCPJP Exam and currently answering Enthuware,

I have a confusion about Anonymous classes,

It is said on enthuware that Anonymous classes cannot be declared static

which is a confusion for me because i can create a static anonymous class like the ff:



it compiles therefore i am confused about this answer on enthuware question "Anonymous classes cannot be declared static."

Enthuware explanation is
Only classes declared as members of top-level classes can be declared static. Such a member is a top-level nested class if it is declared static, otherwise it is a non-static inner class.
Package member classes, local classes(i.e. classes declared in methods) and anonymous classes cannot be declared static.

 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder if this is a language thing. You can use the static modifier, but I don't think it is a static class. A static class (if it were to have a constructor) would allow you to create multiple instances. Your example feels more like a static variable than a static class.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne is right. In OP's example static refers to variable named anonymousObj, not the anonymous class.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:You can use the static modifier, but I don't think it is a static class. A static class (if it were to have a constructor) would allow you to create multiple instances. Your example feels more like a static variable than a static class.



In my opinion, the closest analogy of a static modifier creating a static nested class, would be the static modifier for the method.

When an anonymous inner class is created within an instance method, it can access the variables/methods of the "this" instance, sort of like having the "this" instance being the outer class instance. On the other hand, when an anonymous inner classes is created within a static method, it doesn't have access to a "this" instance, and behaves more like a static nested class.

Henry

 
Enthuware Software Support
Posts: 4857
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per JLS section 15.9.5:


An anonymous class is always an inner class; it is never static.

 
Paul Anilprem
Enthuware Software Support
Posts: 4857
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernesto Hilvano wrote:Hi JavaRanch,





You are declaring the variable anonymousObj to be static. Not the anonymous class.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static inner classes can have static members, non-static inner classes can't. To check you can try to do the same with your anonymous inner class as well:
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Static inner classes can have static members, non-static inner classes can't.



IMO, using a characteristic difference between regular inner classes and static inner (nested) classes, to prove whether anonymous inner classes are "static" or not, is very debatable. Anonymous inner classes are so different from regular inner/nested classes that I can easily make an argument that concludes differently.

For example, non-static inner classes needs an instance of an outer class to be instantiated. And in the cases of instances of anonymous inner classes that are instantiated during static initialization, or within static methods, there are no instances in context that be used as the instance of the outer class.

Paul Anilprem wrote:As per JLS section 15.9.5:

An anonymous class is always an inner class; it is never static.



Agreed. There is no defined behavior for "static" with anonymous inner classes. It is what it is -- and it is different from regular inner/nested classes.

Henry
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic