aspose file tools
The moose likes Beginning Java and the fly likes Static Nested classes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Static Nested classes" Watch "Static Nested classes" New topic
Author

Static Nested classes

Smitha Amresh
Greenhorn

Joined: Jun 13, 2005
Posts: 6
Hi All,

I am studying about static nested classes and I am surprised to know that we can create instances of static nested classes.

public class Test{
public static void main(String[] arg){

Test.staticTest t = new Test.staticTest();
Test.staticTest t1 = new Test.staticTest();

}
public static class staticTest{

}
}


According to my understanding when we append static modifier that means we have a single instance associated with a class not instance of the enclosing object.

Then isn't it logically wrong procedure to create static class instance?? Please explain me how is it possible.


Thanks in advance,
Smitha
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

A static variable in a class is shared by all instances of that class in a JVM for a class loader.
Now, a class definition is different than a variable declaration. A class is declared static so that it can be accessed statically(without an instance) from static/non-static blocks in same/other classes. This does not mean that all instances of that class will be static. It is perfectly legal to define an instance variable of a static class.
You need to understand that it is just the class "definition" that is static.


apigee, a better way to API!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Static Nested classes
 
Similar Threads
Why can't a top-level class use the static keyword?
How all non static member of outer class are available to the non static method in inner class
use of static nested class?
Creating the instance of static nested class.
static class accessing VERY internal question