The inner class instance can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance, while the static nested class can exist without an instance of OuterClass and has no direct access to the methods and fields of its enclosing instance.
The inner class instance is initialized by “parentClassInstance.new InnerClass()”, while the static nested class instance is intialized by “new ParentClass.StaticNestedClass()”.
publicstaticclassInnerTest { publicvoidsay() { // For a static nested class, it cannot access the outer // parent instance variable. System.out.println("Hello!"); } }
publicstaticvoidmain(String[] args) { Test2t=newTest2("Max"); // For a static nested class, new it with "ParentClass.NestedStaticClass" // directly instead of "parentClassInstance.new NestedStaticClass()". InnerTestit=newTest2.InnerTest(); it.say(); } }