views:

239

answers:

7

I recently saw this constructor in a class:

public MyClass(){ }

There were no other constructors.

Is there a reason for this? Java automatically creates a default constructor, so why would you declare one explicitly? Or is this considered good practice in the same way as using braces for single-statement if statements - in case other constructors are added later and you forget that you don't have a default...?

+4  A: 

It doesn't play any role and can safely be deleted.

cherouvim
+11  A: 

A couple minor points that aren't likely to be why you saw it in this case.

  • It gives you something to set a breakpoint on.
  • You could make it non-public

As far as "in case other constructors are added later and you forget that you don't have a default" - that might be a reason, I suppose. But if a non-default constructor were added, any code that used the default constructor would fail to compile, so the guy adding the new constrcutor would generally need to also add a defintion for the default ctor as well.

Then again, I can't think of any particular harm in having the empty ctor defined (though now that I've typed that, I get a feeling that someone might point out some corner of C++ where it could bite you).

Michael Burr
It may have been auto-created by an IDE or left over from deleting a non-default constructor. Also, I have been known to create such constructors on classes I know need a default (i.e. because of some serialization or data binding requirement) and don't want to be bitten by an issue when a second constructor is made later.
Chris Nava
code that is written only for "in case I need to add code for something else later" is a code smell
matt b
@Michael - :) to the set a breakpoint... Although you could really set a breakpoint on the class (public class MyClass...). @matt b - is it always? I've often heard people advocate braces around single-statement ifs because you may just add other statements later assuming they'll be executed as part of the if, thus generating a subtle bug. This example is probably somewhat more far-fetched though, and should be easy to catch.
froadie
@froadie: Good point about the braces, but I feel that is different because it increases readability (another important goal). Plus, two more braces it much less reduncancy than an entire constructor...
sleske
+1  A: 

Because you believe in the second sentence of the zen of python :

Explicit is better than implicit.

Hubert
Is that true? I can think of a lot of annoying consequences of such a statement... (Always explicitly extending Object, for one.)
froadie
I would say it is. At worst, you'll be verbose, but it can't do any harm and it put everything in front of you when looking for a bug or trying to figure out the big picture. In your specific case, I've never been fond of this syntaxic sugar : if my class can produce instances, it needs a constructor, so why hiding it ? My dislike may come from the contrast between the general verbosity of Java and suddenly, special bonus offer : the constructor is included. There is no shame with having a constructor, right ?
Hubert
A: 

Correct me if I'm wrong (I haven't done Java in a while), but doesn't this prevent a call to the parent constructor? In which case the reasoning is obviously that the parent is going to do something automatically that you don't want to happen in this class.

Laila
No, the `super()` call is still implicit. You can't avoid calling a parent constructor in Java.
Michael Myers
@mmyers - even in a parameter constructor? Or just in the default?
froadie
if a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
LB
+1  A: 

Here is what Java Language Specification says:

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

  • If the class being declared is the primordial class Object, then the default constructor has an empty body.
  • Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.

So default constructor will be created anyway. There is no sense to write it if you don't. have to.

IMO you should only do it if you want to change the visibility level of your constructor, i.e. make it private or package protected

eugener
A: 

I was taught that a public default constructor was required by the java bean specification.. The wikipedia page http://en.wikipedia.org/wiki/Java_Bean also lists it as a requirement.

On the other hand, I looked at the Sun/Oracle site and couldn't find mention of it. See http://java.sun.com/developer/onlineTraining/Beans/Beans1/simple-definition.html

Yes, but it exists implicitly, which should be fine for the java bean spec. I don't think java beans require you to explicitly create one, it just needs it to exist
froadie
+2  A: 

Although nothing is gained by adding the constructor explicitly at the technical level, there are potentially reasons to do it. One would be if the class is instantiated via reflection, you may want to put some documentation on the default constructor to indicate that it is required even though adding a new constructor would not cause a compilation error.

Another is that some coding standards prefer it in order to explicitly indicate you thought about what kind of constructor this class is supposed to have.

Yishai
+1 for "class is instantiated via reflection": Some frameworks may do this, and not having a no-arg constructor will break them (at runtime :-( ). But this should *definitely* be documented in the constructor javadocs.
sleske