Best Practice: Don't create instances of Utility classes.

Overview

A utility class is one which only contains static methods. No instance of the class is required and can be confusing is one is created/used.

Avoid creating a Utility class instance

From InetAddress.static
impl = (new InetAddressImplFactory()).create();
The InetAddressImplFactory instance is created for a single method call, however all methods for this class are static, making the instance pointless and possibly confusing.

Using a private constructor for a Utility class is not always enough

A common practice to avoid creating utility class instances is to use a private constructor. The problem being that an instance can still be create in the same or nested classes.

From JMX Utility class
static final JMX proof = new JNX();
private JMX() { }
As JMX only has static methods, you might be wonder what it is used for....

From DescriptorCache
static DescriptorCache getInstance() {
    return instance;
}

public static DescriptorCache getInstance(JMX proof) {
    if(proof != null)
        return instance;
    else
        return null;
}
In this case, the JMX instance is being used as proof, the getInstance() was made from a class which had a handle to the JMX class.

This security by obscurity works provided you prevent the use of reflection here. Otherwise its rather pointless.

The Sun/Oracle JVM has another way to detect this which is perhaps clearer which is Reflection.getCallerClass(num) This is also a hack, but is clearer of the intent IMHO.

Avoid complexity and you usually avoid confusion

From Syncfactory
/* 
 * The variable that represents the singleton instance
 * of the SyncFactory class.
 */
private static SyncFactory syncFactory = null;

/**
 * Creates a new SyncFactory object, which is the singleton
 * instance.
 * Having a private constructor guarantees that no more than
 * one SyncProvider object can exist at a time.
 */
private SyncFactory() {};

/**
 * Returns the SyncFactory singleton.
 * 
 * @return the SyncFactory instance
 */   
 public static SyncFactory getSyncFactory() {
    
     // This method uses the Singleton Design Pattern
     // with Double-Checked Locking Pattern for
     // 1. Creating single instance of the SyncFactory
     // 2. Make the class thread safe, so that at one time
     //    only one thread enters the synchronized block
     //    to instantiate. 
     
     // if syncFactory object is already there
     // don't go into synchronized block and return
     // that object.
     // else go into synchronized block
     
     if (syncFactory == null) {
     synchronized (SyncFactory.class) {
        if (syncFactory == null) {
            syncFactory = new SyncFactory();
        } //end if   
     } //end synchronized block    
     } //end if
     return syncFactory;
}
This is all impressive looking, but pointless stuff. All the fields and methods are static, so no instance is required and it would be less confusing and faster if all this code were replace with a one liner in an enum.

Use enum to create a Utility class

The simplest way to create a singleton is to use an enum (a class with one instance)
enum Singleton {
    INSTANCE;
}
Similarly, the simplest way to create a Utility class is to use an enum as well (a class with no instances.
enum Utility {
    ;
}

Comments

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues