File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static method inside an 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 "Static method inside an inner class." Watch "Static method inside an inner class." New topic
Author

Static method inside an inner class.

Anna s Iyer
Greenhorn

Joined: Aug 21, 2000
Posts: 24
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

Joined: Nov 07, 2000
Posts: 44
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

Joined: Nov 14, 2000
Posts: 104
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


Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
Nijeesh Balan
Ranch Hand

Joined: Oct 09, 2000
Posts: 116
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.


Thanks & Regards,<br />Nijeesh.
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
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
 
jQuery in Action, 2nd edition
 
subject: Static method inside an inner class.