1

I want to know whether I need to initialize(call) all the constructors when I have overloaded constructors in Java.

For instance,

public class Dog {
  int size = 0;

  public Dog() {
    size = 20;
  }

  public Dog(int size) {
    this.size = size;
  }
}

edited:

I understand that when the user calls the constructor without any parameter, public Dog() will be called. Will there be any compile error if I do not initialize all the constructors in a class?

When I create Dog d = new Dog(); it will automatically call public Dog(). But, do I also need to declare Dog d = new Dog(40) ?

I think that it wouldn't make sense if I need to initialize both Dog constructors, but I was wondering if there will be problems in other examples.

6
  • 4
    "I want to know that whether I must initialize all the constructors in the class or it is okay not to call all of them." ?? -- I'm not srue that I understand this statement as the user of the class would only call the most appropriate constructor, nothing more and nothing less. Please clarify. Commented Jul 9, 2015 at 22:43
  • If you don't need any Dogs, then it is OK not to call any Dog constructor. Is that what you are asking? Commented Jul 9, 2015 at 22:46
  • You call whatever constructor you need. You do not need to call all of them. You can even have a class called Dog and never ever call it at all. Imagine that! Commented Jul 9, 2015 at 22:47
  • I see! I wanted to know whether I need to call all the constructors in a class. Thanks a lot! Commented Jul 9, 2015 at 22:48
  • As others have pointed out, you don't need to call all constructors. But you could rather declare the default constructor as public Doc() {this(20);} if your intention is to chain the constructors or re-use code Commented Jul 9, 2015 at 23:56

2 Answers 2

1

I wanted to know whether I need to call all the constructors in a class.

Will there be any compile error if I do not initialize all the constructors in a class?

No.

You don't have to call all of the constructors. You won't get a compilation error1 if you write a constructor, or a method and then don't use it anywhere in your application.

You don't even have to call any of the constructors ... if you don't want any instances of the class. (And there are Java coding patterns where a particular class does not need to be instantiated ... ever.)


1 - However, an automated style checker / bug checker, or someone marking / reviewing your code could well notice redundant constructors and methods and "ping" you for it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Stephen! It answers my question :)
0

Overloading constructors should be thought of this way: it provides you with a way to construct the same object from different input sets. Here is an important rule about constructors:

When overloading a constructor, it is important that the parameters passed to each constructor be sufficient to completely specify and construct the target object (as required by its contract).

Calling a constructor always returns an object as long as there were no exceptions. You cannot invoke a second constructor on an object that has already been instantiated ... because it has already been constructed.

As an example, consider the following class:

public class Pair {

    private final int x
    private final int y

    public Pair() {
        this(0, 0);
    }

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Pair(MyDoublet dbl) {
        this.x = dbl.getX();
        this.y = dbl.getY();
    }
    :
    :
}

There are three constructors. Invoking any of them yields a completely specified Pair object. In fact, if a constructor didn't completely specify the Pair object the compiler would issue an error because, for the Pair class, all the instance fields are final and must be specified in the constructor.

Pair p1 = new Pair();      // p1 has x=0, y=0
Pair p2 = new Pair(1, 3);  // p2 has x=1, y=3
Pair p3 = new Pair(0, 0);  // p3 is the same has p1 even though a
                           // different constructor was used.

TLDR: Overloading constructors allows you to specify the construction of objects in the same class but with different sets of inputs.

1 Comment

Hi Scott, thank you for commenting! I didn't know that a constructor must specify the Pair object when the instance fields are final. It helps a lot!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.