Jtest logo




Contents  Previous  Next  Index

CODSTA.UCDC


Utility classes should not have a default constructor

Description

This rule flags any utility class that has a default constructors.

A utility class only contains static methods and static variables. Because an implicit default constructor is "public" and a utility class is not designed to be instantiated, the "public" default constructor of a utility class should be declared "private".

Example

 package CODSTA;
          
 public class UCDC {
          
     // violation: implicit default constructor is public
         
     public static String UCDC = "UCDC";
          
     public static String getUCDC() {
         return "UCDC";
     }
 }

Repair

 package CODSTA;
          
 public class UCDC {
         
     private UCDC() {}
          
     public static String UCDC = "UCDC";
          
     public static String getUCDC() {
         return "UCDC";
     }
 }

Reference

Bloch, Joshua. Effective Java Programming Language Guide. Addison Wesley, 2001, p. 12


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft