Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize instance members? [duplicate]

Tags:

java

When I define a Java class:

class A {
   private String str = "init method 1";

   public A() {
       str = "init method 2";
   }
}

I can either init str when define it or init it in constructor. My question is what difference of the two methods is? Which method is preferred?

like image 876
TieDad Avatar asked Dec 20 '22 02:12

TieDad


2 Answers

The initialization block values are assigned before constructor assigns them.

So the value init member 1 will be assigned first and then init member 2 will be assigned.

Consider this example from theJavaGeek

class InitBlocksDemo {

    private String name ;

    InitBlocksDemo(int x) {
        System.out.println("In 1 argument constructor, name = " + this.name);
    }

    InitBlocksDemo() {
        name = "prasad";
        System.out.println("In no argument constructor, name = " + this.name);

    }

    /* First static initialization block */
    static {
        System.out.println("In first static init block ");
    }

    /* First instance initialization block  */
    {
        System.out.println("In first instance init block, name = " + this.name);
    }

    /* Second instance initialization block */
    {
        System.out.println("In second instance init block, name = " + this.name);
    }

    /* Second static initialization block  */
    static {
        System.out.println("In second static int block ");
    }

    public static void main(String args[]) {
        new InitBlocksDemo();
        new InitBlocksDemo();
        new InitBlocksDemo(7);
    }

}

This outputs,

In first static init block
In second static int block
In first instance init block, name = null
In second instance init block, name = null
In no argument constructor, name = prasad
In first instance init block, name = null
In second instance init block, name = null
In no argument constructor, name = prasad
In first instance init block, name = null
In second instance init block, name = null
In 1 argument constructor, name = null

The program flows as follows.

  • When program starts executing, the class InitBlocksDemo is loaded into JVM.
  • Static initialization blocks run when class is loaded in the order they appear in the program.
  • Now when execution of static block completed, main method is encountered.
  • The statement new InitBlocksDemo(); causes the no-argument constructor to be invoked.
  • As there is a default call to the super no-argument constructor, control goes to super class i.e. Object class
  • After it has completed, then control comes back to our class and starts giving default values to instance variables. In this case, variable name will be assigned value as null.
  • Now the instance blocks will execute in the order they appear in the program. We have not re-assigned value to name variable yet so it will print null
  • After instance blocks are executed, then control goes to the constructor. Here name = "prasad"; will re-assign a new value hence “prasad” will get printed in the no-argument constructor
  • 9.The statement new InitBlocksDemo(7); causes the one-argument constructor to be invoked. Rest of the process is same. Only difference is that name is not re-assigned a new value hence it will print null
like image 125
Prasad Kharkar Avatar answered Dec 22 '22 15:12

Prasad Kharkar


There is no difference between them, the compiler copies the initialization blocks to the constructors

If you, decompile the generated class file for the class

class A {
    private String str1 = "init method 1";

    private String str2;

    public A() {
        str2 = "init method 2";
    }

    public A(String str2) {
        str2 = str2;
    }
}

you can find

class A
{

    private String str1;
    private String str2;

    public A()
    {
        str1 = "init method 1";
        str2 = "init method 2";
    }

    public A(String str2)
    {
        str1 = "init method 1";
        str2 = str2;
    }
}
like image 43
Arun P Johny Avatar answered Dec 22 '22 16:12

Arun P Johny