JAVA Initialization Order

Source: Internet
Author: User

First, the Order of initialization

no inheritance: Static variables, static initialization blocks > variables, initialization blocks > Constructors

inherited: Static variables, static initialization blocks > parent class Initialization > Subclass initialization

The same level, initialized sequentially in the order in which they appear.

 Packageinitialization; Public classInsect { PublicString field0 = This. Initfield ("Insect: variable 0");  Public StaticString staticField0 = Staticfield ("Insect: Static variable 0");  PublicString field1 = This. Initfield ("Insect: Variable 1"); Static{System.out.println ("Insect: Static out of initialization block"); }         PublicString Field2 = This. Initfield ("Insect: Variable 2"); {System.out.println ("Insect: Initialize block"); }         PublicString field3 = This. Initfield ("Insect: Variable 3"); Insect () {System.out.println ("Insect: Constructors"); }         PublicString Field4 = This. Initfield ("Insect: Variable 4");  Public StaticString staticField1 = Staticfield ("Insect: Static variable 1");  Public Staticstring Staticfield (string s) {System.out.println (s); return" "; }         Publicstring Initfield (string s) {System.out.println (s); return" "; }}
Parent Class
 Package initialization;  Public class extends Insect {    static  {        System.out.println ("Beetle: Static out of initialization block");    }    {        System.out.println ("Beetle: Initialization Block");    }    Beetle () {         System.out.println ("Beetle: Constructor");}    }
sub-class
 Packageinitialization; Public classTest { Public Static voidMain (string[] args) {/** Test No inheritance, initialization order*/        //Insect insect = new insect ();                /** Test inheritance, initialization order*/        //Beetle Beetle = new Beetle ();            }}
Test Class

Ii. Initialization of classes (JVM)

A, load: The bytecode binary . class file is loaded into memory.

b, Connection

1. Verify: Verify that the in-memory bytecode conforms to the JVM specification.

2. prepare: Allocate memory for static variables of the class and initialize default values.

3. parsing: Converting a class's symbolic reference to a direct reference

C, initialize: Assigns the static variable of the class to the defined initial value, not the default value.

Iii. initialization steps for classes

A, if the class is not loaded and connected, it is loaded and connected first.

b, if the class has a direct parent class, and the parent class is not initialized, the immediate parent class is initialized first.

c, if the class has an initialization statement, execute the initialization statements in turn.

PS: Static variables and static initialization blocks can all be considered initialization statements of a class.

Iv. active use and passive use of classes

Active Use:

1. creating an instance of a class

2. access a static variable for a class or interface, or assign a value to the static variable

3. invoking a static method of a class

4. Reflection

5. initialize a subclass of a class

class that is marked as the startup class at 6.JVM startup

Passive use: Other ways of using Java classes do not cause initialization of classes.

example, using ClassLoader to load a class is not an active use of the class and does not cause the initialization of the class.

 Packageinitialization; Public classClassloaderinittest { Public Static voidMain (string[] args) {ClassLoader loader=Classloader.getsystemclassloader (); Try{@SuppressWarnings ("Unused") Class<?> cl = Loader.loadclass ("initialization. CL "); System.out.println ("**********************************"); CL= Class.forName ("initialization. CL "); } Catch(ClassNotFoundException e) {e.printstacktrace (); }    }}classCL {Static{System.out.println ("Cl:init"); }}
Test

JAVA Initialization 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.