IllegalInstantiation

Since Checkstyle 3.0

Description

Checks for illegal instantiations where a factory method is preferred.

Rationale: Depending on the project, for some classes it might be preferable to create instances through factory methods rather than calling the constructor.

A simple example is the java.lang.Boolean class. For performance reasons, it is preferable to use the predefined constants TRUE and FALSE. Constructor invocations should be replaced by calls to Boolean.valueOf().

Some extremely performance sensitive projects may require the use of factory methods for other classes as well, to enforce the usage of number caches or object pools.

Notes

There is a limitation that it is currently not possible to specify array classes.

Properties

name description type default value since
classes Specify fully qualified class names that should not be instantiated. String[] {} 3.0

Examples

To configure the check:

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

Example:

class Example1 {
  class Boolean {
    boolean a;
    public Boolean (boolean a) { this.a = a; }
  }

  void Example1 () {
    java.lang.Boolean[] newBoolArray = new java.lang.Boolean[]{true,true,false};
    Integer[] newIntArray = new Integer[]{1,2,3};
  }

  void Example1 (boolean a, int b) {
    Boolean c = new Boolean(a);
    java.lang.Boolean d = new java.lang.Boolean(a);

    Integer e = new Integer(b);

    Integer f = Integer.valueOf(b);
  }
}
        

To configure the check to find instantiations of java.lang.Boolean and java.lang.Integer:

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalInstantiation">
      <property name="classes"
           value="java.lang.Boolean, java.lang.Integer"/>
    </module>
  </module>
</module>
        

Example:

class Example2 {
  class Boolean {
    boolean a;
    public Boolean (boolean a) { this.a = a; }
  }

  void Example2 () {
    java.lang.Boolean[] newBoolArray = new java.lang.Boolean[]{true,true,false};
    Integer[] newIntArray = new Integer[]{1,2,3};
  }

  void Example2 (boolean a, int b) {
    Boolean c = new Boolean(a);
    java.lang.Boolean d = new java.lang.Boolean(a);
    // violation above, 'Instantiation of java.lang.Boolean should be avoided'
    Integer e = new Integer(b);
    // violation above, 'Instantiation of java.lang.Integer should be avoided'
    Integer f = Integer.valueOf(b);
  }
}
        

Finally, there is a limitation that it is currently not possible to specify array classes:

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalInstantiation">
      <property name="classes"
           value="java.lang.Boolean[], Boolean[], java.lang.Integer[], Integer[]"/>
    </module>
  </module>
</module>
        

Example:

class Example3 {
  class Boolean {
    boolean a;
    public Boolean (boolean a) { this.a = a; }
  }

  void Example3 () {
    java.lang.Boolean[] newBoolArray = new java.lang.Boolean[]{true,true,false};
    Integer[] newIntArray = new Integer[]{1,2,3};
  }

  void Example3 (boolean a, int b) {
    Boolean c = new Boolean(a);
    java.lang.Boolean d = new java.lang.Boolean(a);

    Integer e = new Integer(b);

    Integer f = Integer.valueOf(b);
  }
}
        

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.coding

Parent Module

TreeWalker