Many beginners may not understand the use of static methods, here I say my understanding:
The call to the static method does not depend on the creation class object
The This keyword is not used in the static method because no objects need to be created
Static member variables and static methods can only be called in static methods, because normal methods need to be called by creating objects, which conflicts with static methods
Static member variables and static methods can be called in normal methods and can be called directly from the class name. Static method
A static block of code that executes when the class is loaded and executes only once
The truth is over, look at a practical example:
Class person{ static{ System.out.println ("person static"); } Public person (String str) { System.out.println (' person ' +str);} } public class Test {person person = new Person ("test");p ublic Test () {System.out.println ("test constructor");} static{ System.out.println ("Test static 1"); public static void Main (string[] args) {new MyClass ();} STATIC{SYSTEM.OUT.PRINTLN ("Test static 2");}} Class MyClass extends Test {person person = new Person ("MyClass"); static{ System.out.println ("MyClass Static"); } Public MyClass () { System.out.println ("MyClass constructor");} }
First guess his output, then compare, see what's wrong, deepen understanding
Test static 1test static 2myclass Staticperson Staticperson testtest Constructorperson Myclassmyclass Constructor
First load the test class, which contains two static blocks of code, then output test static 1,test static 2 in writing order
In the main method, new has a MyClass, then loads the MyClass class, and the MyClass class also has a static code block, then the output MyClass static
The MyClass class inherits from the test class, and the test class has already been loaded and no longer outputs the contents of the static code block
After loading, the Test class starts executing, the person class is not loaded when the person is executed, and the person class is loaded with a static block of code, then output person static
Executes the person's constructor, outputting the person Test
Continue to test, enter the main method, new MyClass (), however MyClass inherits from the test class, first executes the test class construction method, output test constructor
Continue execution Myclass,person person = new Person ("MyClass"), execute the Person class constructor method, output person MyClass
Continue with the MyClass construction method, output MyClass constructor
Execution complete
Based on these I summarize the following order of execution:
Static code Blocks---parent class construction Method--child class construction method
When the construction method and the new object exist at the same time, the new object's construction method is executed first. (Be careful not to create loop nesting, which causes memory overflow)
Related articles:
On the loading order of the parent class and subclass in Java
Java inheritance, whether the subclass inherits the constructor of the parent class