Execution order of code in JAVA class
Relationship between static inner classes and inner classes
- Only inner classes can be declared as static classes, that is, static inner classes
- Static classes can only be defined in inner classes
- The static inner class is bound to the outer class. Even if there is no object to create the outer class, it still exists
- Methods of static internal classes can be static or non static. Static methods can be called through static classes in the outer layer, while non static methods can only be called after creating class objects.
- Static internal classes can only reference static member variables (that is, class variables) of external classes
- If an inner class is not defined as a static inner class, it cannot be defined as static when defining member variables or member methods.
summary
- Have static members: static inner classes can have static members (Methods and properties), while non static inner classes cannot have static members (Methods and properties)
- Restrict access to members of external classes: static internal classes can only access static members of external classes, while non static internal classes can access all members (Methods and properties) of external classes
- What are the differences between static internal classes and non static internal classes when they are created:
// Suppose class A has static internal class B and non static internal class C. The difference between creating B and C is: A a=new A(); A.B b=new A.B(); A.C c=a.new C();
Static member / member variable
- Static variables exist in the method area (let's talk about the differences on the Internet: before JDK7, static variables did exist in the method area, but after JDK8, the permanent generation was replaced by the original space, and the string pool and static variables were migrated into the heap. This is the explanation for the inconsistency on the storage location of static variables on the Internet.)
- Member variables exist in the heap
- Member variables exist together with the object. They exist as the object is created and are released as the object is recycled
- Static variables coexist with classes, exist with the loading of classes, and disappear with the disappearance of classes
- The member variable belongs to the object, so it also becomes an instance variable
- Static variables belong to classes, so they are also called class variables
- Member variables can only be called by objects
- Static variables can be called by object or class name
Let's take a look at the execution order of constructor / code block / static code block in the case of subclass inheritance
public class A { private int s1=m1(); private static int s2=m2(); public A(){ System.out.println("1.Construction method of parent class"); } static { System.out.println("2.Static code block of the parent class"); } { System.out.println("3.Code block of parent class"); } public static int m2(){ System.out.println("4.The method called by the static member variable of the parent class"); return 1; } public int m1(){ System.out.println("5.The method called by the member variable of the parent class"); return 2; } } public class B extends A { private int s3=m3(); private static int s4=m4(); public B(){ System.out.println("6.Construction method of subclass"); } static { System.out.println("7.Static code block for subclasses"); } { System.out.println("8.Code block of subclass"); } public static int m4(){ System.out.println("9.The method called by the static member variable of the subclass"); return 1; } public int m3(){ System.out.println("10.The method called by the member variable of the subclass"); return 2; } public static void main(String[] args) { new B(); } }
Execution sequence:
4.The method called by the static member variable of the parent class 2.Static code block of the parent class 9.The method called by the static member variable of the subclass 7.Static code block for subclasses 5.The method called by the member variable of the parent class 3.Code block of parent class 1.Construction method of parent class 10.The method called by the member variable of the subclass 8.Code block of subclass 6.Construction method of subclass
Note: member variables are loaded during object initialization and are assigned in the constructor
When we use the new keyword to create an object
- The JVM will first load the bytecode file of the object's class and save the static attributes in the heap. At this time, these attributes are still the default values corresponding to their types (except for the variables decorated with final static, which will be directly assigned by the JVM)
- Then the JVM will assign values to static variables in these heaps (Note: the order of assigning values to static modified variables is related to the order of your source code declaration)
- After the static attribute in the heap is assigned, the new keyword will apply for a space in the heap to store the object. At this time, the member attribute in the object officially exists in memory, but will be assigned the default value of its type
- After the new keyword opens up memory space in the heap, the JVM will call the construction method of the object area. In the construction method, the heap member attribute begins to be assigned. When the construction method is executed, the member attribute also completes the assignment.
Here, the execution sequence is exactly as follows:
Static of parent class - > static of child class - > non static member of parent class - > construction method of parent class - > non static member of child class - > construction method of child class
The execution order of member variables and code blocks or the execution order of static member variables and static code blocks is related to the order declared in the source code.