views:

157

answers:

9

Checkstyle says:

Class should define a constructor.

PMD says:

Avoid unnecessary constructors - the compiler will generate these for you.

Who is right? Or let's put it this way - what are the pros and cons of having an empty default ctor in a class?

+4  A: 

I like PMD's answer. The less code the better. Don't write constructors the compiler will write for you.

My impression is that the main argument for writing the constructor is that some poor programmer who doesn't understand how constructors work in Java might stumble over your code and get confused. I don't like writing code that is needlessly obscure but I don't like writing dumbed-down code either.

Nathan Hughes
A: 

Although I'm somewhat like lesser code but having default constructor make it easier to set a breakpoint whenever it is needed to debug.

Anyway, may I remind you that PMD's website also said that the issue is controversial :)

nanda
A: 

I'll go with Checkstyle. With no explicit (noarg) constructor, no one will know if it is intended or not.

Consider this utility class:

public class Util {
    // no constructor given - implicit default constructor
    // intended? probably not, but who knows

    public static void foo() {
      // blabla
    }

    public static void bar() {
      // more blabla
    }

}
Helper Method
How can it not be intended? If it is used, and there is no other constructor, then obviously it must have been intended.
DJClayworth
You have to look somewhere else to see if it's used and it might get refactored away in future in any case. I would say explicity declare the default constructor and then if this is a library class with only static methods, mark the constructor private.
andrewmu
@andrewmu Yes, that was my intention.
Helper Method
+4  A: 

When the default constructor is the only constructor, it is 100% equivalent to write it explicitly with an empty body or to omit it. However, the compiler will not generate a default constructor if you have any explicitly defined constructors, default or not. This means that if you rely on the compiler to generate a constructor for you, and later add alternative constructors, then the default constructor goes away. Personally, I would tend toward letting the compiler do the generation anyway; if that default constructor was in use it will generate compile warnings and is easy to add at that point. Otherwise, why keep it around at all?

David Winslow
+1  A: 

By default the compiler generates the default constructor for you, so if you don't want to specify any special actions( initializing of members is not the point here ), then you have not to specify the constructor.

Other thing is that some classes should have a consistent state. For example you have a Book class. There is no sence to create a book with no title, so it is necessary to specify a constructor with string parameter:

public Book(String name) {
    this.name = name;
}

As for default constructors they may be necessary if you should serialize your class or use it in marshalling/unmarshalling(JAXB requires an empty default constructor).

If that's not the point and your class hasn't what is called a consistent state, so empty default constructor is absolutely unnecessary to declare.

You should remember that default constructor is public by default, so consider decalring an explicit one if you want some restrictions to this.

Also if your class is rather long you can consider declaring an empty default constructor to increase readability.

Vladimir Ivanov
+2  A: 

As with many decisions that are 'controversial', the truth is that it really doesn't matter that much. Write the constructor or don't. The effect on the quality and maintainbility of your code will be negligible. If you are coding with others then adopt the same style for consistency, but otherwise - do whichever you feel like.

DJClayworth
+1 - I'm reminded of theological debates on how many angels can dance on the head of a pin ...
Stephen C
+1  A: 

IMO Class should define a constructor because if you rely on default constructors the instance variables will have default values (like zero for integers, null for a String etc). If you want to set instance variables to some default value whenever an object for that class is created defining a constructor by yourself can be a good choice in that case. (Provided you don't wanna use the getter & setter methods)

class Cube {
   private int length;
   private int breadth;
   private int height;

  public Cube() {
      length = 10;
      breadth = 10;
      height = 10;
  }
     ......
     ......
}

Now when you make an object of the class Cube1 all the instance varibales of that object will be set to 10. If you don't use an constructor here your instance variables (length,breadth and height) will have the default values (zero in this case).


Edited : [Added one more thing]

If you are relying on the compiler to generate a default constructor for you and you want to use some constructors that takes argument/s from the user then you must define the default constructor else the compiler will generate error.

Remember : Compiler makes the default constructor for your class only if that class doesn't contains any other constructors.

Chankey Pathak
Well, you could also initialize the fields where you declare them, like `private int length = 10;` which arguably makes things clearer as it ties the field and its default value together.
gustafc
http://goo.gl/IlpZ
Chankey Pathak
A: 

Functionally it depends. Though reflection is always the exception and not the rule, a class cannot be instantiated with Class.newInstance() without an empty constructor. As most will agree, you shouldn't write your classes with reflection in mind, but its good to know that piece of information.

John V.
A: 

One thing I'll add is that if there is no constructor specified, there is an implicit default constructor which code may depend on.

Then when you add a specific constructor, the code may no longer compile - or even worse, the code will not run (may be a reflective dependency on the parameterless constructor - most ORM tools require a default constructor be present for persistent entities).

Which is why you should explicitly add the default constructor if it is required.

Michael Wiles