File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static method inside an inner class.

 
Anna s Iyer
Greenhorn
Posts: 24
  • 0
  • 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.
 
Aparna Narayanan
Ranch Hand
Posts: 44
  • 0
  • 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

 
Suresh Selvaraj
Ranch Hand
Posts: 104
  • 0
  • 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
 
Nijeesh Balan
Ranch Hand
Posts: 116
  • 0
  • 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
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic