Why Default constructor need to declare in POJO file which has Parameterized Constructor while instantiating Object?

懵懂的女人 提交于 2020-01-23 01:17:09

问题


Suppose I have one POJO class User with a constuctor public User(int id, String name){...}. But when I instantiate the User object like User u=new User() with no parameter Eclipse gives error like The constructor User() is undefined. But it works fine when I have no parameterized Constructor. Can someone please explain why It requires to define default constructor?


回答1:


The default (no-parameter) constructor is ONLY provided if you have provided no others. If you define even a single constructor for your class, you MUST use one of the explicitly defined (ie, in your code) constructors to instantiate the object. You can, of course, define your own zero-parameter, empty constructor if that works for what you're trying to do.

Edit: Answer of why?

The compiler provides a default constructor so that the Object can be Instantiated when there are no constructors defined. But if you have defined a parametric constructor, it means that when you create a new instance of that class, its variables should initialized with the parameters you have passed(or do something similar). Without those initializations, the object might not behave in an expected way. Hence the compiler prevents such things from happening by not defining a default constructor(when you have defined one).




回答2:


The no-arg constructor will be automatically added by the compiler if no constructor is provided by the developer. However, as soon as you put your own custom parameterized constructor, the compiler stops adding default constructor for you.

In this scenario, if you still want to use your no-arg constructor, you have to provide it yourself explicitly:

public User() {
}

public User(int id, String name) {
}

The logic behind this is that: if you define your own parametrized constructor, you are declaring that the parameters listed in the constructor is required to construct an object of the class. Therefore you are also implicitly declares if the user of your library do not provide these two parameters, the object shouldn't be able to construct. Thus the compiler will not add the no-arg constructor for you.

If you want to also declare that your class can still work if none of the specified parameters in the parametrized constructor is provided and you (no arg), then you have the explicitly declare that by providing the non-arg constructor yourself.




回答3:


I am giving answer so late, but let's try to share with you what i know:

  1. When you don't provide constructor compiler provides constructor. Why ? Because it is sure you are going to initialize your object with no argument constructor only. So compiler does it for you.
  2. When you provide parameterised constructor, then compiler doesn't know which constructor you will use to initialize your object. So compiler does not provide for you one no-argument constructor. So you have to write explicitly.

    Hope it will help you.




回答4:


The compiler automatically provides a no-argument, default constructor for any class without constructors but if you explicitly provide any constructor with arguments then compiler will not provide a default constructor mainly due to security reasons.

So what you can do is

 public User(int id, String name){...}
 public User(){this(defualtID,defaultName)};



回答5:


Java compiler automatically provides a no-parameter, default constructor for any class without constructors. If there is no constructor defined in your class then Java compiler will add a no parameter constructor in your generated class file. But if there is a constructor with parameter in your class, then you need to write the no-parameter constructor, compiler will not add it.



来源:https://stackoverflow.com/questions/18349848/why-default-constructor-need-to-declare-in-pojo-file-which-has-parameterized-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!