How to initialize instance members?

When I define the Java class:

class A { private String str = "init method 1"; public A() { str = "init method 2"; } } 

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

+4
source share
4 answers

Initialization block values ​​are assigned before the constructor assigns them.

So, first init member 1 will be assigned, 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 conclusion,

 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 proceeds as follows.

  • When the program starts, the InitBlocksDemo class InitBlocksDemo loaded into the JVM.
  • Static initialization blocks are started when the class is loaded in the order in which they are displayed in the program.
  • Now that the execution of the static block is complete, the main method is encountered.
  • In the statement, new InitBlocksDemo(); the no-argument constructor is called.
  • Since there is a default call to the default super -argument constructor, control passes to the super ie Object class
  • After its completion, control returns to our class and begins to assign default values ​​to instance variables. In this case, the variable name will be assigned as null .
  • Now instance blocks will be executed in the order in which they are displayed in the program. We have not yet redefined the value for the name variable to print null
  • After executing the instance blocks, control passes to the constructor. Here name = "prasadam"; will reassign a new value, so "prasad" will be printed in the constructor with no arguments
  • 9. The operator new InitBlocksDemo(7); invokes a constructor call with one argument. The rest of the process is the same. The only difference is that the name is not reset by the new value, so it prints null
+4
source

the difference is when call forwarding occurs. fields are assigned const values ​​before the constructor starts, so if you add this line to your constructor:

 System.out.println(str); 

You will see that before you assign a new constructor in the constructor, you will see the old value.

there is not much difference, moreover, and for use it is mainly personal preference.

personally anythiong I can assign directly as part of the field declaration - this is what I do.

+3
source

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; } } 
+3
source

In my personal experience, everything is about how expensive or detailed is the initialization of an object. Secondly, you can also consider this a lazy creature and an active creature. I usually create objects at the instance variable level if only the constructor is involved. If there are additional calls to initialize the participant in question, then the call will definitely be moved to the constructor or to the method in which it should be initialized.

This is why the factory method template is used to delegate the actual creation of an object to another class.

+1
source

Source: https://habr.com/ru/post/1493135/


All Articles