A static class i.e. created inside a class is called static nested class in java. It cannot access non-static data members and methods. It can be accessed by outer class name.
class TestOuter1{ static int data=30; static class Inner{ void msg(){System.out.println("data is "+data);} } public static void main(String args[]){ TestOuter1.Inner obj=new TestOuter1.Inner(); obj.msg(); } }
In this example, you need to create the instance of static nested class because it has instance method msg(). But you don't need to create the object of Outer class because nested class is static and static properties, methods or classes can be accessed without object.
import java.io.PrintStream; static class TestOuter1$Inner { TestOuter1$Inner(){} void msg(){ System.out.println((new StringBuilder()).append("data is ") .append(TestOuter1.data).toString()); } }
If you have the static member inside static nested class, you don't need to create instance of static nested class.
class TestOuter2{ static int data=30; static class Inner{ static void msg(){System.out.println("data is "+data);} } public static void main(String args[]){ TestOuter2.Inner.msg();//no need to create the instance of static nested class } }