Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a default no argument constructor in Java?

Why do we need a default no argument constructor in many Java related APIs? Like as a general rule all java bean classes or entity classes (JPA etc) or JAX-WS implementation classes require a explicit no argument constructor.

If by default Java provides a no argument constructor then why most of these standards require a explicit constructor?

like image 944
RRR_J Avatar asked Jun 20 '10 06:06

RRR_J


People also ask

Why do we need a no-arg constructor in Java?

In Java, a no-argument constructor is the default constructor and if you don't define explicitly in your program. Then Java Compiler will create a default constructor with no arguments. The purpose is to call the superclass constructor.

Why do we use no-argument constructor?

The arguments of a constructor can only be found by type, not by name, so there is no way for the framework to reliably match properties to constructor args. Therefore, they require a no-arg constructor to create the object, then can use the setter methods to initialise the data.

Why is default constructor needed?

If not Java compiler provides a no-argument, default constructor on your behalf. This is a constructor initializes the variables of the class with their respective default values (i.e. null for objects, 0.0 for float and double, false for boolean, 0 for byte, short, int and, long).

What is a default no-arg constructor?

1. No-argument constructor: A constructor that has no parameter is known as the default constructor. If we don't define a constructor in a class, then the compiler creates a default constructor(with no arguments) for the class.


2 Answers

Java only provides a default no-argument constructor if no other constructors are defined. Thus, if you have other constructors you must explicitly define a no-arg constructor yourself.

These frameworks use the reflection API and look at method names to determine how to set properties. The arguments of a constructor can only be found by type, not by name, so there is no way for the framework to reliably match properties to constructor args. Therefore, they require a no-arg constructor to create the object, then can use the setter methods to initialise the data.

Some frameworks may support @ConstructorProperties as an alternative.

like image 186
OrangeDog Avatar answered Sep 17 '22 22:09

OrangeDog


I believe frameworks that require public nullary constructors do so because they use reflection to instantiate types, e.g. through Class.newInstance().

As to why the default constructor may not work for this case, here's the relevant JLS section:

JLS 8.8.9 Default Constructor

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:

  • if the class is declared public, then the default constructor is implicitly given the access modifier public;
  • if the class is declared protected, then the default constructor is implicitly given the access modifier protected;
  • if the class is declared private, then the default constructor is implicitly given the access modifier private;
  • otherwise, the default constructor has the default access implied by no access modifier.

So in a public class, the default constructor would have the right visibility, but otherwise an explicitly public one must be provided.

like image 36
polygenelubricants Avatar answered Sep 18 '22 22:09

polygenelubricants