Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 990
Vimal Bera Avatar asked Aug 21 '13 05:08

Vimal Bera


People also ask

Does a POJO class have 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.

Why is default constructor needed?

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.

Can we call default constructor from parameterized constructor?

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.

What is the use of default and parameterized constructor?

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.


2 Answers

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).

like image 160
Kon Avatar answered Oct 03 '22 19:10

Kon


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.

like image 37
KKKCoder Avatar answered Oct 03 '22 19:10

KKKCoder