• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 13269
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya thanks alot henry. and your post on the thread regarding compile time constants was really awesome.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic