• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Nested classes  RSS feed

 
Smitha Amresh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!