Creating an object in the constructor

It seems that the result of the following code is the same, so when should I use each?

public class Person { public Person() { this.family = new Family(); } Family family; } 

to

 public class Person { Family family = new Family(); } 

(one scenario I can think of is when you have several constructors and we want to create an instance of the family only inside one of them ... is this the only case?)

+4
source share
6 answers

For class variables [static variable] you cannot use the first, since you want the initialization to be performed only once, and not every time your constructor is called.

For example, variables, secondly, it is only syntactic sugar of the first.
Sometimes you may have to use the second - for the constructor of arguments that you yourself - are passed to your constructor.

+5
source

The latter guarantees that every person will always have a family (awww, this is not so nice). But in the first case, other constructors can be added by another developer who leaves the family uninitialized. This usually implies the latter, shorter version is preferable. But this is not so, because it may be a valid condition for modeling a person as having no family, in which case initialization through the constructor is superior, because you can pass null.

The third option is a basic POJO style (longer, sigh) with the installer provided, in which Family is not initialized until the box is explicitly called. The nicer thing about using this is that you don't need to provide an empty value to the constructor if you are modeling an orphan.

It all depends on how you want to restrict the construction of Person.

+4
source

The first method gives you custom control over how to initialize the class object that you are dealing with each constructor, while the second parameter will initialize the Family object in the same way for all constructors.

The first method is preferable because it allows you to take the Family object into your constructor, allowing you to do things like dependency injection, which is usually good programming practice (and makes testing easier).

In addition, both of them are member variables (not static, as someone else said, for this you need to use a static keyword). Each instance of this class will contain an instance of the Family object in this way.

I would recommend something like this

 public class Person { public Person(Family family) { this.family = family; } Family family; } 

Edit: To respond to the comment that was made about my post (which was accurate, somewhat), there is a difference in which the order of the objects is initialized. However, the comment said that the order of work:
initialization of instance variables -> instance initializer -> constructor

From checking this, it seems that it depends on what happens first in the code, the initialization of the instance variables or instance initializers, and after which the constructor is called after both of them . Consider an example where the constructor of the Family object simply prints the string specified by it:

 public class Person { { Family familyB = new Family("b"); } Family familyA = new Family("a"); Family familyC; public Person() { this.familyC = new Family("c"); } } 

leads to this result when building the person object:
Family: b
Family: a
Family: c

but this code:

 public class Person { Family familyA = new Family("a"); { Family familyB = new Family("b"); } Family familyC; public Person() { this.familyC = new Family("c"); } } 

leads to this result when building the person object:
Family: a
Family: b
Family: c

+3
source

I would recommend using the first when it comes to a subclass. Consider this:

 public class Child extends Person { public Child() { // super(); // or this.family = new PatchWorkFamily(); } } 

With the second method of initialization, you will always initialize a new family and then overwrite it if necessary. This may not be what you want - and it may be bad for performance if this object does a lot of work when instantiating.

+2
source

The second style is terrible if you plan to extend the class (see Daff's answer).

For example, variables, I believe that a maintenance programmer, who is usually a junior programmer, is clearer to do all the instance level initialization in the constructor. I recommend your option 1.

 public class Person { private Family family; public Person() { family = new Family(); } } 

I prefer to avoid this when this is optional, as it adds nothing.

+1
source

I like the second style better. If you have several constructors, you do not need to repeat the initialization, and you will not forget them. In addition, it makes it clear how a variable is initialized. As a rule, when reading a program and crossing a variable, you first proceed to declare it. With the second style, you will immediately see the default value. First you need to look at the constructor.

None of them are superior to computing.

0
source

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


All Articles