This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner classes" Watch "Inner classes" New topic
Author

Inner classes

Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
Hi,

Why Inner classes(i.e Non static) cannot declare static variables (unless its compile time constant) or static intializers.Because of which even member interfaces which are implicitly static cannot be declared in Inner class..
Can somebody explain the concept behind this??

Thanks
Smitha
Thomas Paul Bigbee
Ranch Hand

Joined: Jun 28, 2005
Posts: 71
Static inner classes are really just top level static nested classes, which can exist without an instance of their outer class, however a non-static inner class requires an instance of its enclosing class before it can be instantiated, also remember that their are two types of non static inner-classes, top-level and method. Kathy Sierra's book goes into great detail on this topic, also, you really need to know all about inner-classes to pass the exam, you're not specifically tested on inner-classes, however, quite a few code examples are shown with all types of inner-classes, in order to know what is going on, you'll need to understand inner-classes inside and out.
Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
Hi Thomas,

I have gone through kathy sierra book,I know the types of inner and top level classes but the concept I didnt get is why cant we declare static variable inside inner class.Still not clear on this..


Smitha
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
Smitha,

The basic rule of inner classes is, they are always, always associated with an instance. Because an instance must always exist in order to use an inner class, there's no point to permitting them to have static variables.

Now, why did Sun do it this way? That I couldn't tell you. It's possible that due to the complex nature of inner classes, it was simply too much trouble to support static variables.
Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
Hi Ryan,

I got it now!! Thanks for your simple explanation...

Cheers
Smitha
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
However, it is important to be aware that this particular restricion does not prevent inner (non-static) classes from accessing their parent class static members if they are accessible.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Inner classes