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?
Properties of POJO classIt must have a public default constructor. It may have the arguments constructor. All objects must have some public Getters and Setters to access the object values by other Java Programs. The object in the POJO Class can have any access modifies such as private, public, protected.
What is the significance of the default constructor? They are used to create objects, which do not have any specific initial value. Is a default constructor automatically provided? If no constructors are explicitly declared in the class, a default constructor is provided automatically by the compiler.
You can't call a default constructor once you've created a constructor that takes arguments. You'll have to create the no argument constructor yourself in order to make a call from the parameterized constructor. Yes, this will work.
The default constructor is a constructor that the compiler automatically generates in the absence of any programmer-defined constructors. Conversely, the parameterized constructor is a constructor that the programmer creates with one or more parameters to initialize the instance variables of a class.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With