If you do not need to associate an internal class object with its peripheral class object, you can declare the internal class as static. This is usually called a nested class ). To understand the meaning of static when applied to an internal class, you must remember that a reference is implicitly saved for a common internal class object, pointing to the peripheral Class Object for creating it. However, this is not the case when the internal class is static. Nested class means: 1. To create an object of the nested class, you do not need the object of its peripheral class. 2. Non-static peripheral class objects cannot be accessed from nested class objects.
Public class Outer {private static int I = 1; private int j = 10; public static void outer_f1 () {} public void outer_f2 () {} // static internal classes can be modified using public, protected, and private. // static internal classes can be defined as static or non-static members. static class Inner {static int inner_ I = 100; int inner_j = 200; static void inner_f1 () {// The static internal class can only access static members of the external class (including static variables and static methods) System. out. println ("Outer. I "+ I); outer_f1 ();} void inner_f2 () {// The static internal class cannot access non-static members of the external class (including non-static variables and non-static methods) // System. out. println ("Outer. I "+ j); // outer_f2 () ;}} public void outer_f3 () {// static members of the internal class accessed by the external class: internal class. static member System. out. println (Inner. inner_ I); Inner. inner_f1 (); // non-static members of the internal class accessed by the external class: instantiate the internal class to Inner inner = new Inner (); inner. inner_f2 ();} public static void main (String [] args) {newOuter (). outer_f3 ();}}
Generating a static internal class does not require external class members: this is the difference between the static internal class and the member internal class. Objects of static internal classes can be directly generated: Outer. Inner in = new Outer. Inner (), instead of generating external class objects. In this way, the static internal class is actually a top-level class (normally, you cannot place any code inside the interface, but the nested class can be part of the interface because it is static. Only place the nested class in the interface namespace, which does not violate the interface rules)