wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  declare static members inside inner class or method local inner class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " declare static members inside inner class or method local inner class?" Watch " declare static members inside inner class or method local inner class?" New topic
Author

declare static members inside inner class or method local inner class?

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451

Why can't we declare static members (variable/instance variables) inside an inner class or a method local inner class?

For inner class, is it because we need to have an instance of that class instantiated before we can use the static member ? But according to the syntax, to use a static member, we don't need to instantiate any instance . That is a contradiction.

For method local inner class, is it because the inner class instance will be eligible for garbage collection when the method exits.If we have a static member declared, we cannot use it outside the scope of the method.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

How would you access such a static member?

Outer.Inner.myStatic? This won't work, because Inner is non-static and thus needs to be accessed through an instance of Outer. So would you do:

myOuter.Inner.myStatic? That seems silly. The point of a static variable is that it's in global space. You don't want to access them through instances.

Since the fields are static anyway, you might as well just declare them in the enclosing class directly.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, I guess inner class can be private/protected. Even though we declare a public static variable inside it, this variable cannot be accessed by other classes outside the outer class .
The child class in a different package from its parent class can inherit this inner class, but cannot access this static variable by creating reference Outer.Inner i = new Outer().new Inner() ; And there is a point that child class from a different package can only access its parent's protected variables thru inheritance, not through reference.

Therefore, declaring a static member for inner class does no help.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

but if we declare the static variable as final then we are able to define it in the inner class. any explanation for that ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

gurpeet singh wrote:but if we declare the static variable as final then we are able to define it in the inner class. any explanation for that ?


Well, this is not true -- so, no need to explain..... However, I will speculate that you probably saw an example of a compile time constant (that is declared as a static variable). Compile time constants are allowed, even if the constant variable is a static variable.

For example ....



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

ya thanks alot henry. and your post on the thread regarding compile time constants was really awesome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: declare static members inside inner class or method local inner class?