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
posted
0
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
posted
0
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
posted
0
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
posted
0
Hi Ryan,
I got it now!! Thanks for your simple explanation...
Cheers Smitha
Edwin Dalorzo
Ranch Hand
Joined: Dec 31, 2004
Posts: 961
posted
0
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.