Java Initialization Order

Source: Internet
Author: User

1. Within the class, the order in which the variables are defined determines the order in which they are initialized, even if the definitions of the variables are scattered among the method definitions, they will be initialized before any method (including constructors) is called.

2. Initialization of static data

Class Bowl{bowl (int marker) {print ("Bowl (" +marker+ ")");} void F1 (int marker) {print ("F1 (" +marker+ ")");} Class Table{static Bowl bowl1=new Bowl (1); Table () {Print ("table ()"); Bowl2.f1 (1);} void F2 (int marker) {print ("F2 (" +marker+ ")");} Static Bowl bowl2=new Bowl (2);}} Class Cupboard{bowl Bowl3=new Bowl (3); static Bowl bowl4=new Bowl (4); Cupboard () {print ("cupboard ()"); Bowl4.f1 (2);} void F3 (int marker) {print ("F3 (" +marker+ ")");} Static Bowl bowl5=new Bowl (5);} public class Staticinitialization{public static void Main (String args[]) {print ("Creating new Cupboard () in main"); new Cup Board ();p rint ("Creating New Cupboard () in main"); new cupboard (); Table.f2 (1); cupboard.f3 (1);} Static table Table=new table (), Static cupboard cupboard=new cupboard ();

Output

Bowl (1)

Bowl (2)

Table ()

F1 (1)

Bowl (4)

Bowl (5)

Bowl (3)

Cupboard ()

F1 (2)

Creating New Cupboard () in main

Bowl (3)

Cupboard ()

F1 (2)

Creating New Cupboard () in main

Bowl (3)

Cupboard ()

F1 (2)

F2 (1)

F3 (1)


3. Explicit static initialization

Class Cup{cup (int marker) {print ("Cup (" +marker+ ")");} void f (int marker) {print ("F (" +marker+ ")");}} Class Cups{static Cup Cup1;static Cup cup2;static {cup1=new cup (1); Cup2=new Cup (2);} Cups () {print ("cups ()");} public class Exlicitstatic{public static void Main (String args[]) {print ("Inside main ()"); CUPS.CUP1.F (in);   (1)}//static cups cup1=new Cup ();  (2)//static cups cup2=new Cup ();  (2)}}

Output

Cup (1)

Cup (2)

F (99)


4. Differences in public class and normal class

In the public class, the static variable and the static block are loaded first, and then loaded into the main function without loading non-static blocks and non-static variables

public class Staticblocktest {static Block B1; Block B2 = new Block (2), static block B3=new block (3); {b2 = new Block (2); SYSTEM.OUT.PRINTLN ("Non static Block");} public static void Main (String args[]) {Block b = new block (0);} STATIC{B1 = new Block (1); System.out.println ("static Block");}} Class Block {public block (int marker) {System.out.println ("block (" + marker + ")");}}

Output

Block (3)
Block (1)
Static block
Block (0)

In class, static and non-static blocks are loaded only when the object is instantiated

5. Initialization sequence in inheritance

The parent class static block is loaded first, then the subclass static block is loaded, then the parent class is loaded, then the non-static block is loaded, the constructor is loaded, and the same order is followed in the parent class

public class inheritancetest {public static void main (String []args) {dog d=new dog ();}} Class animal{        static int a=print (4);    int c=print (5);     animal () {        print (0);    }         public static int print (int i) {        system.out.println (i);   & nbsp;    return i;    }}class Dog extends animal{    int b=print (2);     dog () {                 System.out.println ("b=" +b);    }            static{//n  EW Animal (); (1)         system.out.println ("Dog Static");    }} 

Output

4
Dog static
5
0
2
b=2


The parent class can be loaded by creating an instance of the parent class and a super () in the subclass constructor

Assuming that the gaze is removed (1), the output is:

4
5
0
Dog static
5
0
2
b=2








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.