• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner classes

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ryan,

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

Cheers
Smitha
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic