Java Base Static block, non-static block, execution sequence of constructor, java Constructor

Source: Internet
Author: User

Java Base Static block, non-static block, execution sequence of constructor, java Constructor

There are usually some static blocks in Java, which are used for initialization before the class is generated. No matter java or the static block in C ++, it is the first initialization. The structure is as follows:
Static {
Static statement code block
}

{
Non-static statement code block
}
Similarities and Differences
Similarities: both are executed when the JVM loads the class and before the constructor executes it. Multiple classes can be defined. Generally, some static variables are assigned values in the code block.
Differences: static code blocks are executed before non-static code blocks (static code blocks --> non-static code blocks --> constructors ).
The static code block is only executed once in the first new (or as long as it is accessed), and is not executed once in the future, instead of the static code block will be executed once in every new time, just like the constructor. Non-static code blocks can be defined in common methods (personal feelings do not work), but static code blocks do not work.

Java code

Public class TestStatic {static {System. out. println ("base class Static statement block");} public TestStatic () {System. out. println ("No parameter constructor by default for the base class");} {System. out. println ("base class non-static statement block");} public static void Iint () {System. out. println ("base class Static Iint ");}}



Public class User extends TestStatic {private static final long serialVersionUID = 1L; public final double I = Math. random (); // different results are obtained each time. private final static int finalID; private static int finalID2; private int finalID3; static {System. out. println ("derived class Static statement block"); finalID = 1000 ;}{ System. out. println ("derived class non-static statement block"); finalID2 + = 1; finalID3 + = 1;}/** full constructor */public User () {System. out. println ("");} public static void IintTest () {System. out. println ("derived class Static Iint ");}}

Test Statement 1

Public static void main (String [] args ){
TestStatic. Iint (); // User. Iint ();
}
}

The output result is as follows:

Base Static statement Block
Static statement block of a derived class
Base Class Static Iint

The test shows that the static code block is executed no matter how it is. The static method of the base class is called, and the static statement block of the derived class is also executed. The same result is also returned when the User. Iint (); is called.

Test Statement 2

Public static void main (String [] args ){
TestStatic. Iint (); // User. Iint ();
User. IintTest ();
}
}

The output result is as follows:

Base Static statement Block
Static statement block of a derived class
Base Class Static Iint
Derived class Static Iint

The test shows that the static statement block is executed once, but the static method of the derived class is called and executed.

Test Statement 3

Public static void main (String [] args ){

TestStatic. Iint (); // User. Iint ();
User. IintTest ();
System. out. println (User. finalID );
TestStatic user = new User ();

}
}

The execution result is as follows:

Base Static statement Block
Static statement block of a derived class
Base Class Static Iint
Derived class Static Iint
1000
Base non-static statement Block
The base class has no parameter constructor by default.
Non-static statement block of a derived class
The derived class has no parameter constructor by default.

The test shows that non-static code blocks are executed once every new time, which is the same as the number of constructor times.

Test Statement 4

Public static void main (String [] args ){

TestStatic. Iint (); // User. Iint ();
User. IintTest ();
System. out. println (User. finalID );
TestStatic user = new User ();


System. out. println ();
System. out. println ("second initialization of the derived class ");
User user2 = new User ();


System. out. println ();
System. out. println ("the third time the base class is initialized ");
TestStatic user3 = new TestStatic ();

}

The execution result is as follows:

Base Static statement Block
Static statement block of a derived class
Base Class Static Iint
Derived class Static Iint
1000
Base non-static statement Block
The base class has no parameter constructor by default.
Non-static statement block of a derived class
The derived class has no parameter constructor by default.

Second initialization of the derived class
Base non-static statement Block
The base class has no parameter constructor by default.
Non-static statement block of a derived class
The derived class has no parameter constructor by default.

The third time the base class is initialized
Base non-static statement Block
The base class has no parameter constructor by default.

The test shows that the static code block is executed before the non-static code block (static code block --> non-static code block --> constructor ).
The static code block is only executed once in the first new (or as long as it is accessed), and is not executed once in the next time, instead of the static code block will be executed once in every new time, just like the constructor.


Summary:
1. Static code blocks are automatically executed when a class is loaded. Non-static code blocks are automatically executed when an object is created. Non-static code blocks of this class are not executed when no object is created. Sequence: static code block -- non-static code block -- class constructor.
2. In a static method, only other static members (including variables and methods) of the same type can be called directly, rather than non-static members in the category. For non-static methods and variables, you need to create an instance object for the class before using it, but the static method does not need to create any objects before using it.
3. If some code must be executed at project startup, we can use static code blocks that are actively executed and need to be initialized at project startup,

When other programs call an object without creating an object, the static method is required. In this case, the code is executed passively.
Difference: static code blocks are automatically executed;
Static methods are executed only when they are called;
Purpose: static code blocks can be used to initialize the most common variables and objects of some projects. Static methods can be used as code that can be executed without creating objects.

Related Article

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.