Class Design Checks

Checkstyle Logo

VisibilityModifier

Checks visibility of class members. Only static final members may be public; other class members must be private unless property protectedAllowed or packageAllowed is set.

Public members are not flagged if the name matches the public member regular expression (contains "^serialVersionUID$" by default). Note: Checkstyle 2 used to include "^f[A-Z][a-zA-Z0-9]*$" in the default pattern to allow CMP for EJB 1.1 with the default settings. With EJB 2.0 it is not longer necessary to have public access for persistent fields, hence the default has been changed.

Rationale: Enforce encapsulation.

Properties

name description type default value
packageAllowed whether package visible members are allowed boolean false
protectedAllowed whether protected members are allowed boolean false
publicMemberPattern pattern for public members that should be ignored regular expression ^serialVersionUID$

Examples

To configure the check:

<module name="VisibilityModifier"/>
      

To configure the check so that it allows package visible members:

<module name="VisibilityModifier">
    <property name="packageAllowed" value="true"/>
</module>
      

To configure the check so that it allows no public members:

<module name="VisibilityModifier">
    <property name="publicMemberPattern" value="^$"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

FinalClass

Checks that a class which has only private constructors is declared as final.

Example

To configure the check:

<module name="FinalClass"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

InterfaceIsType

Description

Implements Bloch, Effective Java, Item 17 - Use Interfaces only to define types.

According to Bloch, an interface should describe a type. It is therefore inappropriate to define an interface that does not contain any methods but only constants. The Standard class javax.swing.SwingConstants is an example of a class that would be flagged by this check.

The check can be configured to also disallow marker interfaces like java.io.Serializable, that do not contain methods or constants at all.

Properties

name description type default value
allowMarkerInterfaces Controls whether marker interfaces like Serializable are allowed. Boolean true

Examples

To configure the check:

<module name="InterfaceIsType"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

HideUtilityClassConstructor

Description

Make sure that utility classes (classes that contain only static methods) 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() {
        throw new UnsupportedOperationException(); // prevents calls from subclass
    }

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

Examples

To configure the check:

<module name="HideUtilityClassConstructor"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

DesignForExtension

Description

Checks that classes are designed for extension. More specifically, it enforces a programming style where superclasses provide empty "hooks" that can be implemented by subclasses.

The exact rule is that nonprivate, nonstatic methods of classes that can be subclassed must either be

  • abstract or
  • final or
  • have an empty implementation

Rationale: This API design style protects superclasses against beeing broken by subclasses. The downside is that subclasses are limited in their flexibility, in particular they cannot prevent execution of code in the superclass, but that also means that subclasses cannot corrupt the state of the superclass by forgetting to call the super method.

Properties

None.

Examples

To configure the check:

<module name="DesignForExtension"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker


Back to the Checkstyle Home Page