Do I really need to define default constructor in java?

49,138

Solution 1

A default (no-argument) constructor is automatically created only when you do not define any constructor yourself.

If you need two constructors, one with arguments and one without, you need to manually define both.

Solution 2

While all the answers above are correct, it's a bit difficult for new-comers to wrap it in their head. I will try to answer the question anew for new-comers.

The problem that Ayush was facing was in trying to instantiate an Object for a class via a no-arg constructor. This class however has one or more parameterized constructor and this results in a compile time error.

For example, let us create a class Student with a single parameterized constructor and try to instantiate it via the no-arg constructor.

public class Student {

    private String name;
    private int rollNo;

    public Student(String name, int rollNo) {
        this.name = name;
        this.rollNo = rollNo;
    }

    public static void main(String[] args) {
        // The line below will cause a compile error.
        Student s = new Student();
        // Error will be "The constuctor Student() is undefined"
    }
}

Woha! But when we remove the public Student(String name, int rollNo) constructor all-together, the error is gone and the code compiles.

The reason behind this seeming anomaly lies in the fact that Java only provides us with the default (no-arg) constructor when we do not define any constructor for that class on our own.

For example, the following class is supplied with a default contructor:

public class Student {
    private String name;
    private int rollNo;
}

becomes:

public class Student {

    private String name;
    private int rollNo;

    //Default constructor added by Java.
    public Student() {
        super();
    }
}

In other words, the moment we define any parameterized constructor, we must also define a no-arg constructor if we want to instantiate the object of that class via a no-arg constructor.

Also in case of inheritance, a sub-class with no constructors; is supplied one default constructor. This default constructor supplied by Java as above calls the super class's no-arg constructor. If it can't find one, then it will throw an error.

So yes it's always a good choice to define a no-arg/default constructor.

Ref : Oracle Java Tutorial

Solution 3

A no-arg constructor is automatically inserted for you, if you don't write one. This means, if you write a constructor with some parameters, it will be the only constructor you have, so you must pass some values for those parameters to create an instance of it.

Solution 4

This is exactly the expected behavior.

Java automatically generates a default (no arguments constructors) for classes that don't have any constructor.

If you define another constructor (with arguments), default constructor will not be generated. If you still want one, you need to define it yourself.

Solution 5

Whenever you class is complied, if compiler does not find any valid constructor in class (default,parametrized) only then it will substitute auto generated default constructor for your class.

You must have noticed, you can create objects without any default constructor defined in your class, there comes the concept of auto-generated default constructor.As whenever object is created,default constructor is called.

But, If you define Parametrized constructor in your class, that means you restrict the objects to have that parameters

Example:Every employee must have an id.

So,at that time, Compiler will not insert any default constructor there as there is valid constructor in a class.If you need default constructor too, you have to define by yourself.

Share:
49,138
Ayush Goyal
Author by

Ayush Goyal

Updated on October 02, 2020

Comments

  • Ayush Goyal
    Ayush Goyal over 3 years

    It works fine when constructors are not defined, but gives errors if I define a parameterized constructor and not a default one and not passing any values while creating an object. I thought constructors are predefined.

    Why do I need to define a default constructor if I've defined a parameterized constructor? Ain't default constructor predefined?

  • ruhong
    ruhong almost 9 years
    Link to the docs: "You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does."
  • Lucky
    Lucky about 8 years
    And by default Object is the super class of all classes, the no-argument constructor for Object is called?
  • MC Emperor
    MC Emperor over 7 years
    @Lucky All classes indeed implicitly extend Object, so yes, Object() is called.