Initialization of the declaration during initialization in the constructors

Possible duplicate:
Whether to initialize the variable inside the constructor or external constructor

I was wondering what is better and why. Should I initialize the fields of the class when declaring, or should I do this in the constructor? Given that this is a simple one-line initialization.

class Dude { String name = "El duderino"; Dude() { // irrelevant code } } 

against.

 class Dude { String name; Dude() { name = "El duderino"; // irrelevant code } } 

Edit: I am aware of situations where one of the styles will be preferable to the other, as in the case of the execution of the initialization code, which may cause an exception. Here, I mean cases where both styles are absolutely equivalent. Both ways will achieve the same task. What should I use then?

+6
source share
4 answers

If a member can only be set through an accessor (setter method), I prefer the first style. It gives a hint that the initialized value is the default value during construction.

If a member can be specified at build time, I usually pass the default value to the appropriate constructor from the constructor with fewer parameters. For instance,

 final class Dude { private final String name; Dude() { this("El Duderino"); } Dude(String name) { this.name = name; } } 
+4
source

The first is usually used to initialize a static variable and should be used only for this purpose.

In this case, you should use the second method.

Please correct me if I am wrong.

0
source

It is best to declare variables inside the constructor for the sake of consistency. Initializing a variable may require something like a loop or an if-else statement, which cannot be done in a declaration without putting an operation inside a method.

An exception to this rule are static variables that must be declared outside the constructor.

0
source

Single-line declarations cannot contain complex initialization logic.

If you initialize the variable as:

 class AnotherClass { MyClass anObject = new MyClass(); //MyClass() throws a checked exception. } 

then you will find that you cannot specify the initial value in one line. You will need to place such code in the block, which quite obviously is included in the constructor (or in the non-static initialization block):

Using constructor:

 class AnotherClass { MyClass anObject; AnotherClass() { try{this.anObject = new MyClass();}catch(SomeException e){/*handle exception.*/} } } 

Using the initialization block:

 class AnotherClass { MyClass anObject; { try{this.anObject = new MyClass();}catch(SomeException e){/*handle exception.*/} } } 

I found that the latter makes less comprehensible code, because the declaration and initialization are separate from each other, and initialization does not occur in the constructor-encoded constructor (although there is no difference at run time).

The same goes for other complex procedures related to field initialization. For example, if you intend to initialize an Array or Collection and set the contents of the array / collection to some default value, you should do this inside the constructor:

 class AnotherClass { Integer[] integers; AnotherClass() { this.integers = new Integer[10]; for(Integer integer: integers) { integer = Integer.MIN_VALUE; } } } 
0
source

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


All Articles