Why should the default constructor declare in a POJO file that the Parameterized Constructor has when instantiating the object?

Suppose I have one POJO User class with constuctor public User(int id, String name){...} . But when I create an instance of the User object as User u=new User() without the Eclipse parameter gives an error, for example, the User () constructor is undefined . But it works fine when I don't have a parameterized constructor. Can someone explain why this requires defining a default constructor?

+4
source share
5 answers

The default constructor (without parameters) is ONLY provided if you have not provided others. If you define even one constructor for your class, you must use one of the explicitly defined (i.e., in your code) constructors to create the object. You can, of course, define your own null parameter, an empty constructor, if that works for what you are trying to do.

Edit: Answer why?

The compiler provides a default constructor, so the object can be programmed if no constructors are defined. But if you define a parametric constructor, this means that when creating a new instance of this class, its variables must be initialized with the parameters that you passed (or do something like that). Without these initializations, an object may not behave as expected. Therefore, the compiler prevents such events by not defining a default constructor (if you have defined one).

+4
source

The no-arg constructor is automatically added by the compiler if the developer does not provide a constructor. However, as soon as you put your own custom constructor with parameters, the compiler stops adding the default constructor to you.

In this case, if you still want to use the no-arg constructor, you must explicitly specify it:

 public User() { } public User(int id, String name) { } 

The logic of this is as follows: if you define your own parameterized constructor, you declare that the parameters specified in the constructor are needed to create the class object. Therefore, you also implicitly declare if the user of your library does not provide these two parameters, the object cannot be created. Thus, the compiler will not add you the no-arg constructor.

If you also want to declare that your class can still work, if none of the specified parameters is provided in the parameterized constructor, and you (no argument), then you explicitly declare that you create the constructor yourself without arguments.

+2
source

I give an answer so late, but try to share with you what I know:

  • If you do not provide a constructor, then the compiler provides a constructor. What for? Because it is sure that you are going to initialize your object without an argument constructor. Therefore, the compiler does this for you.
  • When you provide a parameterized constructor, the compiler does not know which constructor you will use to initialize your object. Therefore, the compiler does not provide you with a single constructor without arguments. Therefore, you must write explicitly.

    Hope this helps you.

+1
source

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

0
source

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

So what you can do is

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

Source: https://habr.com/ru/post/1498128/


All Articles