• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes

 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan,

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

Cheers
Smitha
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic