18

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

froadie
  • 79,995
  • 75
  • 166
  • 235
  • 4
    A note on semantics, the proper name for such a constructor is a "no-argument constructor". It's only a "default" constructor when the compiler generates it for you by default. – matt b Feb 19 '10 at 02:22

8 Answers8

16

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
  • 333,147
  • 50
  • 533
  • 760
  • 2
    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 Feb 18 '10 at 21:26
  • 3
    code that is written only for "in case I need to add code for something else later" is a code smell – matt b Feb 19 '10 at 02:21
  • @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 Feb 19 '10 at 02:38
  • 1
    @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 Apr 26 '10 at 11:28
6

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

cherouvim
  • 31,725
  • 15
  • 104
  • 153
3

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

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60
3

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
  • 90,445
  • 31
  • 189
  • 263
  • +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 Apr 26 '10 at 11:29
  • In case the class is instantiated via reflection, I would prefer a test case to ensure no-one accidentally breaks this aspect. – Rüdiger Herrmann Mar 28 '17 at 05:27
1

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

Explicit is better than implicit.

Hubert
  • 829
  • 1
  • 8
  • 15
  • Is that true? I can think of a lot of annoying consequences of such a statement... (Always explicitly extending Object, for one.) – froadie Feb 18 '10 at 21:24
  • 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 Feb 18 '10 at 22:02
1

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 Agaev
  • 1,782
  • 1
  • 10
  • 19
  • 8
    No, the `super()` call is still implicit. You can't avoid calling a parent constructor in Java. – Michael Myers Feb 18 '10 at 20:30
  • @mmyers - even in a parameter constructor? Or just in the default? – froadie Feb 18 '10 at 21:36
  • 6
    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. – LB40 Feb 18 '10 at 21:45
0

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

redcayuga
  • 1,241
  • 6
  • 4
  • 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 Feb 18 '10 at 21:39
0

There is actually a good reason to include it not mentioned in these answers:

If the existence of a no-argument constructor is explicitly part of the contract for your object, then it should be explicitly stated.

If you explicitly intend for the client to subclass your code, as in e.g. Java Swing, then its a good idea to explicitly make an accessible no-arg constructor, even if there are no other constructors. Otherwise, if another programmer, in their wisdom, decides to make another constructor, the new explicit constructor will remove the implicit no-arg constructor and break the client code.

If the code is sub classed in your own code base then this will show up as a compiler error, but if your code is to be published as a jar, and sub-classed by the client there will be no compiler error.

Of course, you could argue that if its part of your contract you should have a junit test for that, and you would be right! But if you are working on a large code base where tests take hours to run for your daily build, breaking your daily build because you forgot you had to explicitly make a no arg constructor when adding a constructor is a huge time waste when an explicit constructor and a comment would have avoided any chance of an error.

The goal of good code is to make it as easy as possible for another developer to change it without a mistake!

phil_20686
  • 4,000
  • 21
  • 38
  • *"Otherwise, if another programmer, in their wisdom, decides to make another constructor,"* ... If he has a class without any constructor, then he should be able to understand the impact to the code, if he creates the first (explizit) constructor. (just my two cents) – Tom Jun 29 '15 at 14:22