Java Program Execution Order

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.