Rule Definition
Utility classes are not meant to be instantiated because all the functionalities that they provide are accessible without instantiating the class. Instantiating these classes means that the developer has misused the class. It also consumes memory unnecessarily. Instead, providing a private default constructor ensures that the class is not instantiated.
Remediation
Add a private default constructor to ensure that the class cannot be instantiated.
Violation Code Sample
public class MyClassUtility
{
static public bool MyUtilityFunction() {
// Do Something Usefull
return true;
}
static int UtilityCount;
} // VIOLATION
Fixed Code Sample
public class MyClassUtility
{
static public bool MyUtilityFunction() {
// Do Something Usefull
return true;
}
private MyUtilityClass() { // FIXED
// Avoid instantiation of the class
}
static int UtilityCount;
}
Reference
http://www.javapractices.com/Topic40.cjp
Related Technologies
JEE
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.