Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Static method inside an inner class.  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't I have a static method or static variable inside a non static inner class?.
Am I missing something very obvious?.
Thanks,
Anna S Iyer.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Inner classes may not declare static initializers. Inner classes may not declare static members, unless they are compile-time constant fields.(ie., static final)
Aparna

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here are the points to remember.
1.You cannot have static members inside a non-static Innerclass.
However, non-static Inner classes can have Compile time constants i.e, static final variables.
This is because static members are not associated with an instance of the Outer class. ( static members are also called as class members )
So, if there are static members inside a non-static Inner class then the static members will also be forced to be associated with the instance of the enclosing Outer class which is not correct and may result in compile time errors.
Example:
class Outer
{
class Inner
{
static final int a = 9; //ok - This is a compile time constant
static int b = 27; // static initializer results in compile-time error
}
}

- Suresh Selvaraj
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suresh,
Can you please explain this point in previous post.
"if there are static members inside a non-static Inner class then the static members will also be forced to be associated with the instance of the enclosing Outer class which is not correct and may result in compile time errors."
Thanks in Advance..
Nijeesh.

 
Suresh Selvaraj
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If a variable is defined as static, it means that only one copy of the variable is available for the class and not for every instance of the class.
Example:

class A
{
public static void main (String [] args)
{
...
static int x=9;
...
}
}
In the above example, class A has only one copy of the variable defined as "static int x=9". So instances of A, for example
A newInstance = new A(); will not have a copy of int x=9 since x is defined as static.
Since a non-static Innerclass is associated with every instance of the enclosing Outer class, a static member cannot be placed inside a non-static Inner class.
Thus if a static variable is defined inside a non-static Innerclass then the static variable will also be associated with every instance of the enclosing Outer class and this may lead to compile time error.
- Suresh Selvaraj
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!