Interface:
Declared attributes in the interface automatically become public static final
The method declared in the interface automatically becomes public
Declared classes in the interface automatically become public static
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 the nested class (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, when the internal
This is not the case when the class is static. Nested classes mean:
1. To create nested class objects, you do not need the objects of its peripheral class.
2. Non-static peripheral class objects cannot be accessed from nested class objects.
Public class Z1 {
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 or non-static members can be defined in the static internal class.
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 ();
System. Out. println ("non-static method ");
}
}
Class innerz {
Int inner_j = 200;
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 ();
System. Out. println ("non-static tired non-static method ");
}
}
Public void outer_f3 () {// static member 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.
Inner inner = new inner ();
Inner. inner_f2 ();
}
Public static void main (string [] ARGs ){
New Z1 (). outer_f3 ();
Z1.inner z = new z1.inner ();
Z. inner_f2 ();
Z1 z1 = new Z1 ();
Z1.innerz ZZ = z1.new innerz ();
ZZ. inner_f2 ();
}
}
Raw
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. This actually makes the static internal class 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)