开发者

Do I really need to define default constructor in java?

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 defin开发者_StackOverflowe a default constructor if I've defined a parameterized constructor? Ain't default constructor predefined?


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.


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


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.


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.


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.


There is also one curious case when you must define non argument constructor. As the other wrote, if you don't specify default constructor - Java will do it for you. It's good to understand how "default generated by Java" constructor looks like. In fact it calls constructor of the super class and this is fine. Let's now imagine one case. You are creating Vehicle class:

public class Vehicle {
private String name;
private String engine;

public Vehicle(String name, String engine) {
    this.name = name;
    this.engine = engine;
}

public String makeNoise(){
    return "Noiseee";
} 
}

As we can see Vehicle class has got only one defined 2 arguments constructor. Now let's create Car class which inheritates from Vehicle class:

public class Car extends Vehicle {

@Override
public String makeNoise() {
    return "Wrrrrrrr....";
}  }

Maybe it looks strange but only one reason why wouldn't it compile is fact that Java cannot create default constructor for Car class which call super Vehicle class. Vehicle class doesn't have no argument constructor and it cannot be generated automatically while 2 arg constructor already exists.

I know that it's very rare case, but I found it as a interesting to know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜