Always listen to the students and teachers have talked about some of the Java program execution process and execution sequence, has never been a summary of their own system. Today, a sudden interest, summed up, but also to make their own school notes.
First, before you begin to understand some of the program's loading sequence processes, first look at the common components of an object. variables, constants, static variables, constructor bodies, static code blocks, code blocks, method bodies. What happens to the memory of the program when we instantiate an object new () in the code? What is the order in which these changes occur?
First, before the instance of an object, the JVM will start to do some preparation for the load, static variables, constants will be loaded into the memory of the method area, constants into the constant pool in the method area. In the real new one, the order of execution of the object is: Static code block--"code block--" constructor.
Code Listing 1:
Public classchildclass{StaticString SST = "Static Variable"; String Str= "Child class member variable assignment"; PublicChildClass () {System.out.println ("I am a subclass construct"); } {System.out.println ("Subclass code Block"); } Static{System.out.println ("Subclass Static code block"); } Public voidGetstr () {System.out.println ("Subclass Method--->" +str); } Public Static voidMain (string[] args) {childclass cc=NewChildClass ();}}
Execution Result:
Sub-class static code block
Subclass code Block
I'm a sub-class construct.
In particular, the problem with static blocks of code is that the same class, if it is performing multiple instances, only executes the static code block for the first time.
Public Static void Main (string[] args) { new childclass (); New ChildClass ();}
Execution Result:
Sub-class static code block
Subclass code Block
I'm a sub-class construct.
Subclass code Block
I'm a sub-class construct.
There is a extends relationship between classes and classes, and if there is a relationship, what is the order of execution of the program? Don't say more, look at the code first
Code two:
Public classChildClassextendsfatherclass{StaticString SST = "Static Variable"; String Str= "Child class member variable assignment"; PublicChildClass () {System.out.println ("I am a subclass construct"); } {System.out.println ("Subclass code Block"); } Static{System.out.println ("Subclass Static code block"); } Public Static voidMain (string[] args) {childclass cc=NewChildClass (); }}
Code Three:
Public class Fatherclass { = "Parent class member variable assignment"; Public Fatherclass () { System.out.println ("I am the parent class construct"); } { System.out.println ("Parent class code block"); } Static { System.out.println ("Parent class static code block"); } }
Execution Result:
Parent class static code block
Sub-class static code block
Parent Class code block
I'm the parent construct.
Subclass code Block
I'm a sub-class construct.
If the same object continues to instantiate, the static code block for the parent class and subclass is still executed only once.
Java Program Execution Order