Initialization order of classes in Java

Source: Internet
Author: User

Initialization Order of the one class (no inheritance condition)
Rules:
1. Static variables > General variables > Construction methods
2. The order in which variables are defined determines the order in which they are initialized

3. Static variables are the same as static blocks, and normal and non-static blocks are the same, i.e. static blocks can be thought of as static variables. Think of non-static blocks as normal variables

public class Test1 {public static void main (string[] args) {new D ();}} Class C{public C (String s) {System.out.println (s);}} The construction method of class d{/* D */public D () {System.out.println ("constructor of D");} /* Non-static variable */private c C1 = new C ("Variable 1");p rivate c C2 = new C ("Variable 2");/* static variable */private static C C3 = New C ("Static variable 1");p Riv Ate static c C4 = new C ("Static variable 2");/* static initialization block of */STATIC{SYSTEM.OUT.PRINTLN ("Static initialization block of" D.

。 ");} /* Instance initialization block for */{system.out.println ("D" Instance initialization block: ");}}


Operation Result:

Static variable 1
Static variable 2
Static initialization block of D.


Variable 1
Variable 2
An instance of D initializes the block.


How to construct D


Initialization order of the second class (with inheritance)

Rules:
1. Initialization of the parent class > Initialization of subclasses
2. Static variables > General variables > Construction methods
3. The order in which variables are defined determines the order in which they are initialized
4. Static variables are the same as static blocks, and normal variables and non-static blocks are the same. It is possible to think of static blocks as static variables and non-static blocks as normal variables.

public class Test2 {public static void main (string[] args) {new B ();}} Class Feild{public Feild (String s) {System.out.println (s);}} Class a{/* A construction method */public a () {System.out.println ("constructor method of the parent class");} /* A non-static variable */private feild f1 = new Feild ("Parent class variable 1");p rivate feild F2 = new Feild ("Parent class Variable 2");/* static variable */private static feild F3 = new Feild ("Parent class static variable 1");p rivate static feild f4 = new Feild ("Parent static variable 2");/* Static initialization block */STATIC{SYSTEM.OUT.PRINTLN ("Static initialization of the parent class") Block:

");} /* Instance of a initializes block */{system.out.println ("instance initialization block of the parent class.

。 ");}} The construction method of Class B extends a{/* b */public B () {System.out.println ("constructor method of the subclass");} /* Non-static variable */private feild f5 = new Feild ("Subclass variable 1");p rivate feild f6 = new Feild ("Subclass variable 2");/* static variable of b */private static feild F7 = new Feild ("Subclass static variable 1");p rivate static Feild F8 = new Feild ("Subclass static Variable 2");/* static initialization block for/* b */static{system.out.println ("Static initial of subclass") Block: ");} /* Instance of B initializes block */{system.out.println ("instance initialization block of subclass: ");}}


Operation Result:

Parent class static Variable 1
Parent class static Variable 2
Static initialization block for parent class:


Sub-class static variable 1
Sub-class static variable 2
Static initialization block for subclasses:
Parent class Variable 1
Parent class Variable 2
Instance initialization block for parent class:


How to construct a parent class
Sub-class Variable 1
Sub-class Variable 2
Initializes a block of instances of the child class:
How to construct subclasses



Look at the following example:

public class Test1 {public      static void Main (string[] args) {          new D ();      }  }      the constructor of class d{/* d      /public D () {          System.out.println ("Construction method of D");      }            Public D (String s) {          System.out.println (s);      }        /* Non-static variable for d *      /private static D D1 = new D ("Variable 1");      private static D D2 = new D ("Variable 2");            /* static initialization block for D      /* static{          System.out.println ("Static initialization block for" D ".

。 "); } /* Instance initialization block * /{System.out.println ("D" instance initialization block.

"); } }


The output is:

Instance initialization block for D:
Variable 1
Instance initialization block for D:


Variable 2
Static initialization block of D.


An instance of D initializes the block.


How to construct D


Parsing: Because of new D (), two instances are created in class D. And because the static block's position is behind the two instance creation, the contents of the static block are not output first. Instead, two instances were created D1, D2, and then New D (). The static block, instance block, and construction method are then run.


The following moves the static blocks to D1 and D2 before they are created:

public class Test1 {public      static void Main (string[] args) {          new D ();      }  }      the constructor of class d{/* d      /public D () {          System.out.println ("Construction method of D");      }            Public D (String s) {          System.out.println (s);      }        /* static initialization block for D      /* static{          System.out.println ("Static initialization block for" D ".

。 "); } /* Non-static variable for d * /private static D D1 = new D ("Variable 1"); private static D D2 = new D ("Variable 2"); /* Instance initialization block * /{System.out.println ("D" Instance initialization block:

"); } }


The output results are as follows:

Static initialization block for D:
An instance of D initializes the block.


Variable 1
Instance initialization block for D:
Variable 2
Instance initialization block for D:
How to construct D



Initialization order of classes in Java

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.