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 and static blocks are the same, ordinary variables and non-static blocks are the same, that is, static blocks can be considered as static variables, non-static blocks as ordinary 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 */STATIC{SYSTEM.OUT.PRINTLN ("Static initialization block" of "D"). ");} /* Instance initialization block for */{system.out.println ("D" Instance initialization block: ");}}

Execution Result:

Static variable 1
Static variable 2
Static initialization block for D:
Variable 1
Variable 2
Instance initialization block for D:
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 and static blocks are the same, ordinary variables and non-static blocks are the same, that is, static blocks can be considered as static variables, non-static blocks as ordinary 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: ");}}

Execution 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






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.