1

When I have a class with multiple constructors where one constructor calls another one (constructor chaining), which constructor should be the one that all the other constructors call eventually?

Here's an example where the "all args" constructor calls the "no args" constructor:

class Goblin {
    // always starts at 10
    private int strength;
    private String name;

    public Goblin() {
        this.strength = 10;
    }

    public Goblin(String name) {
        this();
        this.name = name;
    }
}

Here's an example where the "no args" constructor calls the "all args" constructor

class Goblin {
    // always starts at 10
    private int strength;
    private String name;

    public Goblin() {
        this(null);
    }

    public Goblin(String name) {
        this.name = name;
        this.strength = 10;
    }
}

The idea is that there should be 1 place where this.strength = 10 and it won't have to be copied. Which of these is best?

3
  • If you use an instance variable initializer, you won't need constructor chaining, at least for this example. The next step would be to have a constructor that offers a strength parameter, then you might entertain constructor chaining.
    – Erik Eidt
    Mar 23, 2018 at 17:24
  • Right. For this example, it could be just private int strength = 10;. I was just using that logic as a stand-in for potentially more complicate logic, e.g. setting up internal arrays for a hash table implementation that takes a capacity argument, so we can't simply use an instance initializer there. Mar 23, 2018 at 18:02
  • How about separating the concerns of initialization of data and of allocating the components necessary for the object? Mar 23, 2018 at 18:15

4 Answers 4

5

I would advocate for preferring the second approach, as this allows you to declare both members as final and make the class formally immutable.

1
  • 2
    Good point about the immutability. And it seems this would be true even if we had, say, Goblin(int), Gobling(String), and the "all args" constructor as Goblin(int, String). If we don't want to expose the "all args" constructor, we can just make it private I guess. Mar 23, 2018 at 15:14
2

In general, it doesn't matter. The important thing is to chain the constructors so you're not repeating code. But if the no-arg one calls the one with arguments, or the one with args calls the no-arg one, they both accomplish the same thing - creating a (valid) object with no code repetition.

2

Your first approach only works as long as your default values and the default values for the datatype match.

If for example all goblins constructed with an empty constructor should be named "John", you need the second approach, otherwise you have this.name = in your code twice.

0

You should call the constructor with more args. This is called Constructor Delegation in C++ and the purpose it to reuse as much code as possible.

1
  • Having everything call the "no args" constructor also re-uses code, though. Or, if there's a 1-arg and a 2-arg constructor, the 2-arg one can call the 1-arg one and the 1-arg one can call the no arg one. Mar 23, 2018 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.