Relationships between static inner classes and inner classes
- Only inner classes can be declared static, that is, static inner classes;
- Static classes can only be defined in inner classes;
- A static inner class is bound to an outer class, which exists even if no object of the outer class is created.
- Static methods can be static or non-static methods. Static methods can be called from the outer layer of the static class, whereas non-static methods must be called after the class object is created.
- Static inner classes can only refer to static member variables of external classes.
- If an inner class is not defined as a static inner class, it cannot be defined as static when defining a member variable or method.
Conclusion:
- Can have static members or not: Static inner classes can have static members (methods, properties), while non-static inner classes cannot have static members (methods, properties).
- Are there restrictions on access to members of an external class? Static inner classes can access only static members of the external class, while non-static inner classes can access all members of the external class (methods, attributes).
- Static inner classes and non-static inner classes are created differently:
// If class A has static inner class B and non-static inner class C, create the difference between B and C:
A a=new A();
A.B b=new A.B();
A.C c=a.new C(a);
Copy the code
Static variable/member variable
- Member variables exist in heap memory.
- Static variables exist in the method area.
- Member variables live and die with objects. They exist when the object is created and are released when the object is reclaimed.
- Static variables live and die with the class. They exist as the class is loaded and disappear as the class disappears.
- Member variables are also called instance variables because they belong to objects.
- Static variables are also called class variables because they belong to a class.
- Member variables can only be called by objects.
- Static variables can be called by objects as well as by class names.