Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are fields initialized before constructor code is run in Java?

Can anyone explain the output of following program? I thought constructors are initialized before instance variables. So I was expecting the output to be "XZYY".

class X {     Y b = new Y();      X() {         System.out.print("X");     } }  class Y {     Y() {         System.out.print("Y");     } }  public class Z extends X {     Y y = new Y();      Z() {         System.out.print("Z");     }      public static void main(String[] args) {         new Z();     } } 
like image 661
Praveen Kumar Avatar asked Feb 11 '13 03:02

Praveen Kumar


People also ask

Do fields need to be initialized before use?

Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.

Is constructor a first place a variable is initialised?

A constructor is typically used to initialize instance variables representing the main properties of the created object. If we don't supply a constructor explicitly, the compiler will create a default constructor which has no arguments and just allocates memory for the object.

How do you initialize a field in a constructor in Java?

The way to initialize class fields is with something called a static initializer. A static initializer is the keyword static followed by code in curly braces. You declare a class field much as you would declare a local variable.

Should I initialize variables in constructor Java?

If you know what value a final variable will have at declaration, it makes sense to initialize it outside the constructors. However, if you want the users of your class to initialize the final variable through a constructor, delay the initialization until the constructor.


2 Answers

The correct order of initialisation is:

  1. Static variable initialisers and static initialisation blocks, in textual order, if the class hasn't been previously initialised.
  2. The super() call in the constructor, whether explicit or implicit.
  3. Instance variable initialisers and instance initialisation blocks, in textual order.
  4. Remaining body of constructor after super().

See sections §2.17.5-6 of the Java Virtual Machine Specification.

like image 59
user207421 Avatar answered Oct 13 '22 06:10

user207421


If you look at the decompiled version of the class file

class X {     Y b;      X() {         b = new Y();         System.out.print("X");     } }  class Y {     Y() {         System.out.print("Y");     } }  public class Z extends X {      Y y;      Z() {         y = new Y();         System.out.print("Z");     }      public static void main(String args[]) {         new Z();     } } 

You can find that the instance variable y is moved inside the constructor, so the execution sequence is as follows

  1. Call the constructor of Z
  2. It triggers the default constructor of X
  3. First line of X constructor new Y() is called.
  4. Print Y
  5. Print X
  6. Call the first line in constructor Z new Y()
  7. Print Y
  8. Print Z

All the instance variables are initialized by using constructor statements.

like image 40
Arun P Johny Avatar answered Oct 13 '22 05:10

Arun P Johny