Java class initialization sequence

Source: Internet
Author: User

I divided the initialization sequence of Java classes into two parts. The first part is the class initialization, and the second part is the object initialization.

Before initialization, the JVM must first load the bytecode of this class into the memory. At this time, no matter whether an object is created or not, it will be executed immediately.Class initialization:

Execute static initialization expressions (such as static int A = 10) and static initialization blocks (that is, content in static {}).Source codeIn sequence.

When an object is createdObject initialization:

First, call the constructor of the parent class.

Step 2: Execute the initialization expression (for example, int A = 10) and the content of the initialization block ({}).CodeIn sequence.

Finally, execute your own constructor.

For static initialization expressions and initialization expressions, if they are only declared and not initialized, the default value is assigned. For example, int I; the I value is 0.

Summary:

1. class initialization

(1) Static initialization expression, static initialization Block

2. object initialization

(1) constructor of the parent class

(2) initialization expression, initialization Block

(3) constructor

Note: The initialization expression may not be an accurate term, but you only need to understand what it means.

Test code:

Code Package Principle;

Public ClassInitializetest {

Public   Static   Void Main (string [] ARGs) Throws Classnotfoundexception {
System. Out. println (
" ----- Class initialization ----- " );
Class. forname (
" Principle. myclass " );

System. Out. println (
" \ N "   +   " ----- Object initialization ----- " );
Myclass
=   New Myclass ();
}
}

Class Myclass Extends Father {

Myclass (){
System. Out. println (
" 3. Constructor " );
}

Static   Int Staticint =   5 ; // If it is placed after static {}, the compilation fails.

Static {
System. Out. Print (
" 1. Static initialization expression: staticint = "   + Staticint +   " \ T " );
System. Out. println (
" Static initialization Block " );
}

Int Instanceint; // The default value 0 will be assigned.

{
System. Out. Print (
" 2. Initialization expression: instanceint = "   + Instanceint +   " \ T " );
System. Out. println (
" Initialization Block " );
}

}

ClassFather {
Father (){
System. Out. println (
"1. constructor of the parent class");
}
}

 

Output result:

----- Class initialization -----

1. Static initialization expression: staticint = 5 static initialization Block

----- Object initialization -----

1. constructor of the parent class

2. Initialization expression: instanceint = 0 initialization Block

3. constructor

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.