I've been noticing code recently that makes it clear the author was a C/C++
programmer unfamiliar with Java. Whenever I see these things, I am fixing
them on the spot:

(1) initializers are not required for class or instance members when their
initial values are null, false, 0, etc;

e.g.

public class Foo {
  private Bar bar = null;
  private boolean done = false;
  private int value = 0;
}

should be written as:

public class Foo {
  private Bar bar;
  private boolean done;
  private int value;
}

(2) it is never necessary to invoke super() in a constructor, e.g., super()
in the following is redundant:

public class Foo {
  public Foo() {
    super();
  }
}

(3) it is never necessary to define a default constructor if there is no
other defined constructor and it does nothing; e.g.,

public class Foo {
  public Foo() {
  }
}

should not define a default constructor; Java will always supply a default
constructor if no other constructor is defined;

(4) however, if you want to prevent the generation of a default, public
constructor, then you can define a private no-argument constructor:

public class CantInstantiate {
  private CantInstantiate() {
  }
}

Reply via email to