« AVAILABLE FROM OTN: Oracle Development Kit for Spring | Main | SOLUTION: When to specify an empty default constructor! »

QUIZ: When to specify an empty default constructor in Java?

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.

Frank Nimphius:

You don't need to do this unless you create a singleton in which case you want to make it private

Frank Nimphius:

.. ah, yes - if you want to perform initialization

A default constructor is needed for a class to conform to the JavaBeans specification.

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

Sanjay:

Empty default constructor requires when you have overloaded constructors and programmer wants to create object as like with default constructor.

Angus:

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

sivakiran kolli:

when no constructor is written in java,java compiler creates a default constructor with default values.

sivakiran kolli:

it's ok

Robbie Vanbrabant:

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.

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

voofunny:

You need to provide empty constructor in the parent class if your child class is having the constructor with an argument

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.

vinod kumar:

When we provide a parametric constructor in any program it is compulsory that we should provide Default Constructor otherwise Compiler generate Compile time error

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About This Entry

This page contains a single entry from the blog posted on May 24, 2007 6:40 AM.

The previous post in this blog was AVAILABLE FROM OTN: Oracle Development Kit for Spring.

The next post in this blog is SOLUTION: When to specify an empty default constructor!.

Many more can be found on the main index page or by looking through the archives.

Top Tags

Powered by
Movable Type and Oracle