This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner classes" Watch "Inner classes" New topic

Inner classes

Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99

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??

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..

Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69

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...

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.
I agree. Here's the link:
subject: Inner classes
Similar Threads
Anonymous inner /static initializator
Member class can have static variables??
which one is correct
Inner Class Problem