I'm a purist. I don't like unnecessary things as the distract my attention from the important stuff...
Browsing the blogosphere, magazines, presentations, and books I see a lot of sample code which makes me shudder. The reason is simple. The usage of the Java Default Constructor. To make you aware of the good usage of it, I'm challenging you with this quiz:
When to specify an empty default constructor in Java?
Please post your answers in the comments section.I'll summary the answers next week.
Comments (14)
When there also is a need for one (or more) constructors with arguments. So unless there are other constructors, I don't write no arg constructors.
Posted by Peter Veentjer | May 24, 2007 1:28 AM
Posted on May 24, 2007 01:28
You don't need to do this unless you create a singleton in which case you want to make it private
Posted by Frank Nimphius | May 24, 2007 2:45 AM
Posted on May 24, 2007 02:45
.. ah, yes - if you want to perform initialization
Posted by Frank Nimphius | May 24, 2007 2:45 AM
Posted on May 24, 2007 02:45
A default constructor is needed for a class to conform to the JavaBeans specification.
Posted by Jörn Horstmann | May 24, 2007 2:50 AM
Posted on May 24, 2007 02:50
Also, when your class is non-instantiable and you want to enforce it (see Items 2 and 3 in Effective Java). It's necessary to declare the no-arg constructor in order to change its access modifier to private (or possibly protected or default).
It annoys me that many IDEs (I think JDeveloper included, sadly) generate a public no-arg ctor in new classes by default...
Posted by Brian Duff | May 24, 2007 3:44 AM
Posted on May 24, 2007 03:44
Empty default constructor requires when you have overloaded constructors and programmer wants to create object as like with default constructor.
Posted by Sanjay | May 25, 2007 1:41 AM
Posted on May 25, 2007 01:41
Hi Olaf
I could think of the following three scenarios where you absolutely HAVE to have a create a default constructor:
1. When you really do want to do some initialisation in the constructor.
2. When you have extended a class and need to make sure constructor chaining is in place i.e. you want to ensure that the superclass is initialised properly.
3. When you want to prevent or restrict construction. Prevent construction by making the constructor private or limit it's use by making in protected or default scoped.
Otherwise I don't bother. Do I win the prize?
Angus
Posted by Angus | May 25, 2007 7:18 AM
Posted on May 25, 2007 07:18
when no constructor is written in java,java compiler creates a default constructor with default values.
Posted by sivakiran kolli | May 25, 2007 9:46 PM
Posted on May 25, 2007 21:46
it's ok
Posted by sivakiran kolli | May 25, 2007 9:48 PM
Posted on May 25, 2007 21:48
If you use it, make that explicit by defining it! Because if that code is not properly unit tested, you could introduce problems if you add another.
Posted by Robbie Vanbrabant | May 27, 2007 6:40 AM
Posted on May 27, 2007 06:40
I would always explicitly define it, that is if you want it to be enabled for use.
So I would go further then just the technical requirements like, for instance, Angus stated.
krgds,
Kevin
Posted by Kevin Smeyers | May 29, 2007 8:33 AM
Posted on May 29, 2007 08:33
You need to provide empty constructor in the parent class if your child class is having the constructor with an argument
Posted by voofunny | August 19, 2008 3:31 PM
Posted on August 19, 2008 15:31
As stated above, Java creates a default constructor if no is defined. You don't have to create a default consturctor in the parent class if the child constructor call any of the available parent constructors.
Posted by Olaf Heimburger | August 19, 2008 3:56 PM
Posted on August 19, 2008 15:56
When we provide a parametric constructor in any program it is compulsory that we should provide Default Constructor otherwise Compiler generate Compile time error
Posted by vinod kumar | September 19, 2008 8:16 AM
Posted on September 19, 2008 08:16