Java initialization sequence Review

Source: Internet
Author: User

Differences between static final, static, and final

Static: static, global, and class objects share a static variable, only initialized once.

Final: constant. If it is a method, it cannot be overwritten. If it is a class that cannot be inherited, it must be set during initialization. After setting, it cannot be changed.

Static final: static variable

The initialization sequence is as follows:

Parent static variables (static initialization blocks)> subclass static variables (static initialization blocks)> parent common variables (Common initialization blocks)> parent class constructor> parent class common methods

> Subclass common variable (Common initialization block)> subclass constructor> subclass common method

The following is an example to illustrate all this.

Package com. loulijun. test1;

Public class test1 {
Public static void main (string ARGs [])
{
Son son = new son ();
Son. Say ();
}
}

Class father
{

// Static final and static are not executed sequentially. They are executed from top to bottom in the code order.
// Static variables
Public static string STR = initstr ("FATHER: static variable --- 1 ");
// Static constant
Public static final int I = initfinal (2 );
// Common variable
Public Int J = Init (4 );

Static int initfinal (int I)
{
System. Out. println ("FATHER: static constant ---" + I );
Return I;
}
Static int Init (Int J)
{
System. Out. println ("FATHER: Common variable ---" + J );
Return J;
}
// Static initialization Block
Static
{
System. Out. println ("FATHER: static initialization block --- 3 ");
}
Static string initstr (string Str)
{
System. Out. println (STR );
Return STR;
}

// Initialize the block
{
System. Out. println ("FATHER: normal initialization block --- 5 ");
}
// Constructor
Public father ()
{
System. Out. println ("FATHER: parent class constructor --- 6 ");
}

Public void say ()
{
System. Out. println ("FATHER: normal method --- 7 ");
}

}

Class son extends father
{
// Static final and static are not executed sequentially. They are executed from top to bottom in the code order.
// Static variables
Public static string STR = initstr ("Son: static variable --- 1 ");
// Static constant
Public static final int I = initfinal (2 );
// Common variable
Public Int J = Init (4 );
Static int initfinal (int I)
{
System. Out. println ("Son: static constant ---" + I );
Return I;
}
Static int Init (int I)
{
System. Out. println ("Son: normal variable ---" + I );
Return I;
}
// Static initialization Block
Static
{
System. Out. println ("Son: static initialization block --- 3 ");
}
Static string initstr (string Str)
{
System. Out. println (STR );
Return STR;
}

// Initialize the block
{
System. Out. println ("Son: normal initialization block --- 5 ");
}
// Constructor
Public son ()
{
System. Out. println ("Son: subclass constructor 6 ");
}

Public void say ()
{
System. Out. println ("Son: normal method --- 7 ");
}
}

Running result:

Father: static variable --- 1
Father: static constant --- 2
Father: static initialization block --- 3
Son: static variable --- 1
Son: static constant --- 2
Son: static initialization block --- 3
Father: Common variable --- 4
Father: Common initialization block --- 5
Father: parent class constructor --- 6
Son: Common variable --- 4
Son: normal initialization block --- 5
Son: subclass constructor 6
Son: common method --- 7

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.