By Doug Lowe

Sometimes you want to create a Java class that can’t be instantiated at all. Such a class consists entirely of static fields and methods. A good example in the Java API is the Math class.

Its methods provide utility-type functions that aren’t really associated with a particular object. You may need to create similar classes yourself occasionally. You might create a class with static methods for validating input data, for example, or a database access class that has static methods to retrieve data from a database. You don’t need to create instances of either of these classes.

You can use a simple trick to prevent anyone from instantiating a class. To create a class instance, you have to have at least one public constructor. If you don’t provide a constructor in your class, Java automatically inserts a default constructor, which happens to be public.

All you have to do to prevent a class instance from being created, then, is provide a single private constructor, like this:

public class Validation

{

private Validation() {} // prevents instances

// static methods and fields go here

}

Now, because the constructor is private, the class can’t be instantiated.

Incidentally, the Math class uses this technique to prevent you from creating instances from it. Here’s an actual snippet of code from the Math class:

public final class Math {

/**

* Don’t let anyone instantiate this class.

*/

private Math() {}

If this trick is good enough for the folks who wrote the Math class, it’s probably good enough for you.