Initialization steps for the [Java] class

Source: Internet
Author: User

Objective

Class initialization process, which can be carried out in a multithreaded environment, in order to simplify, this article describes the single-threaded case of class initiation steps.

In addition, the order of inheritance is not the focus of this article, this article focuses on the initialization order of the internal fields of a class.

The term "initialize" is specifically intended for a class. When an instance is created, the instance field is initially assigned, not called initialization, but is directly referred to as the creation of the instance to differentiate.

Body

Initialization of the class, including initialization of static code blocks, initialization of static fields (fields of the Class).


The initialization trigger condition for the class:

1) T is a class, and an instance of T is created.

2) A static method of T is called

3) A static field of T is assigned value

4) A static field of T is used, and the field is not a literal constant.

5) T is a class, and an inline assertion statement is executed

This article uses the 4th trigger condition to demonstrate.

Initialization steps for a class:

1. Before a class is initialized, that is, before any class fields of non-literal constants are initialized, the literal class constants are initialized first, such as field static final stirng a = "good".

2. When the class is triggered to initialize, if its immediate parent class is not initialized, the immediate parent class is initialized, and if the immediate parent class is not initialized, the immediate parent class of the immediate parent class is initialized, and so on, until the Object class or a certain level of grandparent class has been initialized.

3. Initialize all class fields of non-class constants while initializing static code blocks, in text order.

Example

Super, parent class, initialized with output

Test, the subject class, inherit Super, has four fields: Literal class constant field1, non-literal class constant Field2, class variable field3, instance variable field4; Has a static block of code that is executed when the class is initialized.

The inner class of test$innerclass,test, which is called when Test is initialized, is used to indicate that the literal class constant, non-literal class constant, is initialized for a different time.

Initdemo, demonstrates the time step of the class.

Specific code

Super, parent class

 Public class Super {    static  {        System.out.println ("initializing Super");}    }

Test, the subject class of the tests

 Public classTestextendsSuper { Public Static FinalString field1 = "Test.field1";  Public Static FinalString field2 = Innerclass.pint ("Initializing test.field2");  Public StaticString field3 = Innerclass.pint ("Initializing test.field3");  PublicString field4 = Innerclass.pint ("Initializing test.field4"); Static{System.out.println ("Initializing Test class"); System.out.println ("\ t" + field1 + "-" + Field2 + "-" +field3); }         PublicTest () {System.out.println ("In Test ()"); }         Public voidBmethod () {System.out.println ("In Test.bmethod ()"); }         Public Static classinnerclass{ Public Staticstring Pint (string s) {System.out.println (s); returnS.substring (S.indexof ("") + 1); }                Static{System.out.println ("Initialzation in Test$innerclass"); System.out.println ("\ t" + field1 + "-" + Field2 + "-" +field3); }    }}

Initdemo, demonstrating the steps of Test initialization

Public class Initdemo {public    static void Mian () {        System.out.println (test.field1);        System.out.println ("----------");        System.out.println (test.field2);    }}

The output is as follows

1 Test.field12----------3 Initializing Super4InitialzationinchTest$innerclass5Test.field1-NULL-NULL6 Initializing test.field27 Initializing test.field38 Initializing Test class9Test.field1-test.field2-test.field3TenTest.field2

From the output you can see:

When the output literal constant Test.field1, the Test class is not initialized. The initialization of Test is triggered when the output of a non-literal class constant test.field2.

Before the Test class is initialized, its parent class Super is first initialized.

Line 5th is the output of the inner class Test$innerclass, before the Test class is initialized, the non-literal class field filed2, field3 are null, and only the literal class constant field1 has been assigned.

The 9 row is the initialization run of the Test class static code block, at which time the class field Field2, field3 have been initialized.

The initialization of a class is simply an assignment of the class field (Field1, Field2, field3), the instance field Field4 is not initialized, and it needs to wait until the instance is created to be initialized.

Resources

12.4. Initialization of Classes and Interfaces, the Java Language specification, Java SE 8 Edition

What's the best-implement constants in Java?, StackOverflow

Initialization steps for the [Java] class

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.