The order of loading of class files
1. First load the static variables and static initialization blocks of the parent class (the execution order is arranged in order)
2. Reload the static variables and static initialization blocks that execute this class
As long as the class is not destroyed, the static variables and static initialization block will only be executed once, and these two steps will not be performed further in the subsequent operation of the class.
Class instance creation process
An instance of the class is created only when the new method is called.
1. In the order of loading of the class file above (skip this step if the class has been loaded)
2. Non-static variables and non-static initialization blocks of the parent class
3. The construction method of the parent class
4. Non-static variables and non-static initialization blocks of this class
5. The construction method of this class
4. When destroying a class instance, first destroy the subclass part and then destroy the parent class part.
Both static methods and non-static methods are passively called
That is, the system will not call and execute automatically. Therefore, the user does not execute it when it is not called. The main difference is that static methods can be called directly with the class name (and instantiation objects can also be done), while non-static methods can only be called after instantiating the object.
Related concepts
static keyword:
is a modifier used to modify members (member variables and member functions)
The modified members have the following characteristics:
Load as the class is loaded (as soon as the class is loaded, the static data will immediately load space in memory)
Disappears as the class disappears, indicating that it has the longest life cycle
Preferentially over object existence (object disappears, static is still there)
Static existence first, object exists later
Shared by all objects
Save memory space
When a member is statically modified, in addition to being called by the object, it can also be called directly by the class name.
Writing: Class name. Static member
Notes on usage
Static methods can only access static members (methods and variables)
Non-static methods can access both static and non-static
This and super keywords cannot be written in static methods
Because static precedence over object, this cannot appear in static methods
The main function is static
publicstaticvoidmain(String[]args){}
When to use static?
We need to start from two aspects: because the content of static modification includes member variables and functions.
When to define a static variable (class variable)
When shared data appears in the object, the data is statically modified. The unique data in the object should be defined as non-statically existing in heap memory.
When to define a static function
When non-static data (object-specific data) is not accessed internally, the function can be defined as static.
Static pros and cons
profit:
1. Store shared data in separate spaces for objects, saving space. There is no need to store one copy of each object.
2. Can be called directly by class names
Disadvantages:
1. Life cycle is too long
2. Access limitations occur (only static access)
Memory structure
When Java programs are running, they need to allocate space in memory. In order to improve computing efficiency, different areas of the space have been divided, because each area has a specific data processing method and memory management method.
Stack memory
Used to store local variables When the data is used, the space occupied will be automatically released.
Heap memory
Arrays and objects (entities), instances created through new are stored in heap memory (member variables are established with the establishment of the object and exist in the heap memory where the object is located). Each entity has a memory address value (variables are referenced by address). Variables in the entity have default initialization values. The entity is no longer used and will be recycled by the garbage collector within an uncertain time (garbage collection mechanism)
Method area, local method area, register
verify
| Loading order | Parent class static variable = 1 | Parent class non-static variable = 1 | Subclass static variable = 1 | Subclass non-static variable = 1 |
|---|---|---|---|---|
| [The parent class calls the parent class static method] | Parent.pStaticMethod(); | |||
| Parent class static initialization block one | 2 | |||
| Parent class static initialization block two | 3 | |||
| Parent class static method | 4 | |||
| [Subclass calls subclass static methods] | Child.cStaticMethod(); | |||
| Subclass static initialization block one | 5 | 2 | ||
| Subclass static initialization block two | 6 | 3 | ||
| Subclass static methods | 7 | 4 | ||
| 【Subclass Instantiation】 | Child c=new Child(); | |||
| The parent class is non-static initialization block one | 8 | 2 | ||
| The parent class non-static initialization block two | 9 | 3 | ||
| Parent class constructor | 10 | 4 | ||
| Subclass non-static initialization block one | 11 | 5 | 5 | 2 |
| Subclass non-static initialization block two | 12 | 6 | 6 | 3 |
| Subclass construction method | 13 | 7 | 7 | 4 |
| 【Parent class instantiates subclass object】 | Parent p=new Child(); | |||
| The parent class is non-static initialization block one | 14 | 2 | ||
| The parent class non-static initialization block two | 15 | 3 | ||
| Parent class constructor | 16 | 4 | ||
| Subclass non-static initialization block one | 17 | 5 | 8 | 2 |
| Subclass non-static initialization block two | 18 | 6 | 9 | 3 |
| Subclass construction method | 19 | 7 | 10 | 4 |
| Loading order | Parent class static variable = 1 | Parent class non-static variable = 1 | Subclass static variable = 1 | Subclass non-static variable = 1 |
|---|---|---|---|---|
| 【Subclass Instantiation】 | Child c=new Child(); | |||
| Parent class static initialization block one | 2 | |||
| Parent class static initialization block two | 3 | |||
| Subclass static initialization block one | 4 | 2 | ||
| Subclass static initialization block two | 5 | 3 | ||
| The parent class is non-static initialization block one | 6 | 2 | ||
| The parent class non-static initialization block two | 7 | 3 | ||
| Parent class constructor | 8 | 4 | ||
| Subclass non-static initialization block one | 9 | 5 | 4 | 2 |
| Subclass non-static initialization block two | 10 | 6 | 5 | 3 |
| Subclass construction method | 11 | 7 | 6 | 4 |
| 【Parent class instantiates subclass object】 | Parent p=new Child(); | |||
| The parent class is non-static initialization block one | 12 | 2 | ||
| The parent class non-static initialization block two | 13 | 3 | ||
| Parent class constructor | 14 | 4 | ||
| Subclass non-static initialization block one | 15 | 5 | 7 | 2 |
| Subclass non-static initialization block two | 16 | 6 | 8 | 3 |
| Subclass construction method | 17 | 7 | 9 | 4 |
| [The parent class calls the parent class static method] | Parent.pStaticMethod(); | |||
| Parent class static method | 18 | |||
| [Subclass calls subclass static methods] | Child.cStaticMethod(); | |||
| Subclass static methods | 19 | 10 |
public class ClassTest { public static void main (String args[]) { System.out.println("【Subclass instantiation】|Child c=new Child();"); Child c=new Child(); System.out.println("【Premium class instantiates subclass object】|Parent p=new Child();"); Parent p=new Child(); System.out.println("[Premium class calls the parent class static method]|Parent.pStaticMethod();"); Parent.pStaticMethod(); System.out.println("[Subclass calls subclass static method]|Child.cStaticMethod();"); Child.cStaticMethod(); }} public class ClassTest2 { public static void main (String args[]) { System.out.println("[Presiding class calls parent class static method]|Parent.pStaticMethod();"); Parent.pStaticMethod(); System.out.println("[Subclass calling subclass static method]|Child.cStaticMethod();"); Child.cStaticMethod(); System.out.println("[Subclass instantiation]|Child c=new Child();"); Child c=new Child(); System.out.println("【parent class instantiates child class object】|Parent p=new Child();"); Parent p=new Child(); }} public class Parent { // Parent class static variable static int m = 1; // Parent class non-static variable int n = 1; // Static statement block 1 static { m++; // j++; Parent class non-static variable cannot be used in static statement block System.out.println("Preparent class static initialization block 1|" + m); } // Static statement block 2 static { m++; System.out.println("Preparent class static initialization block 2|" + m); } // Constructor public Parent() { m++; n++; System.out.println("Preparent class constructor|" + m + "|" + n); } // Nonstatic statement block { m++; n++; System.out.println("Presiding class non-static initialization block 1|" + m + "|" + n); } // Nonstatic statement block { m++; n++; System.out.println("Presiding class non-static initialization block 2|" + m + "|" + n); } // Nonstatic method public void pMethod() { m++; n++; System.out.println("Presiding class non-static method|" + m + "|" + n); return; } // Static method public static void pStaticMethod() { m++;// j++; The parent class non-static variable cannot be used in static methods System.out.println("Presiding static method|" + m); return; } @Override protected void finalize() throws Throwable { super.finalize(); System.out.println("Destroy the parent class|"); }} public class Child extends Parent { // Static variable static int i = 1; // Nonstatic variable int j = 1; // Static statement block 1 static { m++; i++; // j++; Nonstatic variables cannot be used in static statement block System.out.println("Subclass static initialization block 1" + "|" + m + "||" + i); } // Static statement block 2 static { m++; i++; System.out.println("Subclass static initialization block 2" + "|" + m + "||" + i); } // Constructor public Child() { m++; n++; i++; j++; System.out.println("Subclass constructor" + "|" + m + "|" + n + "|" + i + "|" + j); } // Nonstatic statement block { m++; n++; i++; j++; System.out.println("Subclass non-static initialization block one" + "|" + m + "|" + n + "|" + i + "|" + j); } // Nonstatic statement block { m++; n++; i++; j++; System.out.println("Subclass non-static initialization block two" + "|" + m + "|" + n + "|" + i + "|" + j); } // Nonstatic method public void pMethod() { m++; n++; i++; j++; System.out.println("Subclass inherits non-static methods" + "|" + m + "|" + n + "|" + i + "|" + j); return; } // Static method public static void pStaticMethod() {// Static method cannot be inherited m++; i++; // j++; Nonstatic variables cannot be used in static methods; } // Nonstatic method public void cMethod() { m++; n++; i++; j++; System.out.println("Subclass non-static method" + "|" + m + "|" + n + "|" + i + "|" + j); return; } // Static method public static void cStaticMethod() { m++; i++; // j++; Non-static variables cannot be used in static methods System.out.println("Subclass static method" + "|" + m + "||" + i); return; } @Override protected void finalize() throws Throwable { super.finalize(); System.out.println("Destroy subclass|"); }}Summarize
The above is all the content of this article about the comprehensive analysis of the class loading process in Java. I hope it will be helpful to everyone. If you have any questions, you can leave a message at any time and the editor will reply to everyone in time. Looking forward to your valuable comments.