Monday 5 February 2018

Useless private constructors

public class NullArgumentException extends ApplicationException {

  private NullArgumentException(String message) 
  {
    super(message);
  }

  private NullArgumentException(String message, Throwable cause) 
  {
    super(message, cause);
  }

  public NullArgumentException(String field) 
  {
    super(String.format("No value for field %s", field));
  }

}

I found the code written above in our code base somewhere. My IntellIJ immediately started complaining about it.

I read through it, and then I started complaining about it too.

It looks like someone thought Constructors in Java are inherited, and wished to prevent people from instantiating the class using those constructors.

I removed the offending constructors and made the code a little cleaner.

No comments:

Post a Comment