Question

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?

Was it helpful?

Solution

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.

But this is me being obsessive and probably unreasonable. There is a world of application programmers whose central focus is on the business, not on the language, and who are not language experts. A survival technique a lot of people use is to have a consistent style, whether it is absolutely necessary is not the point.

OTHER TIPS

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.

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?

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 :)

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
    }

}

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top