HideUtilityClassConstructor

Since Checkstyle 3.1

Description

Makes sure that utility classes (classes that contain only static methods or fields in their API) do not have a public constructor.

Rationale: Instantiating utility classes does not make sense. Hence, the constructors should either be private or (if you want to allow subclassing) protected. A common mistake is forgetting to hide the default constructor.

If you make the constructor protected you may want to consider the following constructor implementation technique to disallow instantiating subclasses:

public class StringUtils // not final to allow subclassing
{
  protected StringUtils() {
    // prevents calls from subclass
    throw new UnsupportedOperationException();
  }

  public static int count(char c, String s) {
    // ...
  }
}
        

Properties

name description type default value since
ignoreAnnotatedBy Ignore classes annotated with the specified annotation(s). Annotation names provided in this property must exactly match the annotation names on the classes. If the target class has annotations specified with their fully qualified names (including package), the annotations in this property should also be specified with their fully qualified names. Similarly, if the target class has annotations specified with their simple names, this property should contain the annotations with the same simple names. String[] {} 10.20.0

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="HideUtilityClassConstructor"/>
  </module>
</module>
        

Example:

// violation below, 'should not have a public or default constructor'
@java.lang.Deprecated
class Example1 {

  public Example1() {
  }

  public static void fun() {
  }
}

class Foo {

  private Foo() {
  }

  static int n;
}

class Bar {

  protected Bar() {
    // prevents calls from subclass
    throw new UnsupportedOperationException();
  }
}

@Deprecated // violation, 'should not have a public or default constructor'
class UtilityClass {

  static float f;
}
// violation below, 'should not have a public or default constructor'
@SpringBootApplication
class Application1 {

  public static void main(String[] args) {
  }
}
        

To configure the check to ignore classes annotated with SpringBootApplication or java.lang.Deprecated.

<module name="Checker">
  <module name="TreeWalker">
    <module name="HideUtilityClassConstructor">
      <property name="ignoreAnnotatedBy"
        value="SpringBootApplication, java.lang.Deprecated" />
    </module>
   </module>
</module>
        

Example:

// ok below, skipped by annotation
@java.lang.Deprecated
class Example2 {

  public Example2() {
  }

  public static void fun() {
  }
}

class Foo2 {

  private Foo2() {
  }

  static int n;
}

class Bar2 {

  protected Bar2() {
    // prevents calls from subclass
    throw new UnsupportedOperationException();
  }
}

@Deprecated // violation, 'should not have a public or default constructor'
class UtilityClass2 {

  static float f;
}
// ok below, skipped by annotation
@SpringBootApplication
class Application2 {

  public static void main(String[] args) {
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.design

Parent Module

TreeWalker