Static member class-static member class, which is an internal class containing the "static" keyword in the class declaration.
The following example defines four "static member classes ":
Public class Outer {// 1/4-public permission public static class inner1 {public inner1 () {staticmethod (); // OK! Static member inner class can access static method of outer class // instancemethod (); // compile error: static member inner class can not access instance method of outer class }}// 2/4-default permission static class inner2 {}// 3/4-private permission Private Static class inner3 {// 4/4 nesting define public static class inner4 {}}// 1/2-static method of the external class Private Static void staticmethod () {// cannot define an inner class in a method/* Public static class inner4 () {}*/} // 2/2-instance method of the external class private void instancemethod () {// inner3 is a private static member class. Its scope of use is only built in the predefined domain inner3 inner3 = new inner3 (); inner3.inner4 inner4 = new inner3.inner4 ();}} class test {outer. inner1 inner1 = new outer. inner1 (); outer. inner2 inner2 = new outer. inner2 (); // test and outer are in a package, so inner2 can access // outer. inner3 inner3 = new outer. inner3 (); // compile error: inner3 is private}
--Features:
The static member class can access any static field or static method of the external class-the static word is mainly reflected here
Like static methods or static fields, you have the public, private, and default permissions.
--Constraints:
The static member class cannot be the same as the external class
Like static methods of external classes, you cannot access instance fields and instance methods of external classes.
Static member classes can only be defined in the top-level code of the external class or the top-level code of other static member classes of the external class (nested definition). They cannot be defined in a function of the external class.
--When to use:
Exists as an aid to external classes