• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya thanks alot henry. and your post on the thread regarding compile time constants was really awesome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic