38

A common thing to do to utility classes is to give them a private constructor:

public final class UtilClass {
    private UtilClass() {}

    ...
}

But unfortunately, some tools don't like that private constructor. They may warn that it's never called within the class, that it's not covered by tests, that the block doesn't contain a comment, etc.

A lot of those warnings go away if you do this instead:

public enum UtilClass {;
    ...
}

My question is: besides the unending hatred of future developers, what important differences are there between an enum with no values and a class with a private constructor in Java?

Note that I am not asking What's the advantage of a Java enum versus a class with public static final fields?. I'm not deciding between whether a list of things should be a bunch of constants or an enum, I'm deciding between putting a bunch of functions in a constructor-less class or a value-less enum.

Also note that I don't actually want to do this. I just want to know the trade-offs as part of general language knowledge.

For example, using an enum pollutes the autocomplete with useless methods like UtilClass.values(). What other downsides are there? Upsides?

15
  • 11
    The main downside I see is that it's confusing. A developer expects an enum to be an enum, and to have instances, not a utility class masquerading as an enum.
    – JB Nizet
    Commented Oct 28, 2014 at 21:39
  • 16
    I think the unending hatred of future developers is a strong enough reason to avoid such thing. It feels as a workaround to avoid tool warnings. If this pattern becomes common enough, the tools will eventually warn against it too.
    – afsantos
    Commented Oct 28, 2014 at 21:40
  • 7
    Actually, an empty enum should trigger a more severe warning than a private default constructor.
    – mabi
    Commented Oct 28, 2014 at 21:42
  • 5
    Enums are an under-rated feature of Java. They have all sorts of uses that most would never think of, and in my opinion the fact that most developers artificially limit their capabilities is not a good reason for you to do the same. That being said, it's not what I normally do.
    – Floegipoky
    Commented Oct 28, 2014 at 21:42
  • 3
    As a side note, I usually fill the empty constructor with throw new AssertionError(). It's assertively (?) wrong to ever instantiate an utility class.
    – afsantos
    Commented Oct 28, 2014 at 21:42

4 Answers 4

33

Using enum for something that's not actually an enum is ugly and confusing. I'd say that's enough reason not to do it.

The "utility class" pattern is perfectly legitimate. If your tools don't like it, that's a problem with the tools.

31

One benefit is that you're absolutely guaranteed that no instances can be created, even from inside the class.

A disadvantage is that this goes beyond the usual intent of enums. However, we already do this for singletons implemented with enums.

This bit from "Effective Java" by Joshua Bloch about such singletons also applies to utility classes:

...you get an ironclad guarantee that there can be no instances besides the declared constants. The JVM makes this guarantee, and you can depend on it.

Disclaimer: I have not used this pattern, and am not recommending for or against it.

6
  • 1
    Singleton with enums have a huge disadvantage of being serializable and if you have 2 options for an enum (cfg based) - you might end up (has happened!) with 2 distinct singletons. Singletons should never be serialized, but resolve on use.
    – bestsss
    Commented Oct 30, 2014 at 21:29
  • 1
    This seems tangential to the OP's question. That said, I'm not sure I understand what harm you see caused by allowing a singleton to be serialized. From the JLS: "the special treatment by the serialization mechanism ensures that duplicate instances are never created as a result of deserialization." IIUC, you could load the class and deserialize in multiple class loaders - but then the enum approach is no worse than others. Commented Oct 30, 2014 at 21:55
  • try smth like this enum FactoryImpl implements SuperDuperFactory{ FastFactory{...}, MemoryEfficientFactort{....} }. Now you have 2 possible instances of SuperDuperFactory but you can configure to use one at a time. However if you deserialize an instance with a reference to SuperDuperFactory you might end up with 2 active factories. If would have been an error NotSerializableException if the factory was not serializable. Of course the field reference to the factory should have been transient but such mistakes happen and enum helps mask 'em,since the're invisible if the factory is the same
    – bestsss
    Commented Oct 30, 2014 at 22:00
  • A singleton is a class with exactly one instance. You appear to have two instances. It looks more like you're using the strategy pattern than the singleton pattern. That seems unrelated to the OP's question. Commented Oct 30, 2014 at 22:21
  • Here is an example of what I talk about: pastebin.com/LDLyMikx there is exactly one instance of ByteBufferFactory. There could be different impl. based on the system configuration but effectively there would be a single impl... unless there is a different type gets to be desirialized. Pay attention that the enum is actually not publicly visible, so there is no other way to get a different instance.
    – bestsss
    Commented Oct 31, 2014 at 12:17
9

The following pattern in utility classes also provides ironclad guarantee that there can be no instances:

public abstract class Util {
    private Util() { throw new Error(); }
    ... // static methods
}

Besides, you have no additional irrelevant static methods, provided by enums.

13
  • 1
    Yes, but how many people will read in the specs they are not supposed to call that constructor? Some people I know wasted hours before finding out that what they did was against the code contracts. I would advice making the constructor private. Commented Oct 28, 2014 at 22:17
  • 1
    Right, but that's not the question. Also... ironclad? If you don't mark the class final, you can perform child resurrection with the finalizer (similar to this). Commented Oct 28, 2014 at 22:22
  • 1
    "Ironclad" is a little strong. Deserialization could create an instance, even with this constructor. And additional constructors could be added. Commented Oct 28, 2014 at 22:29
  • @AndyThomas: Good point, If one hacks into the java byte code one can derive the "class code" and let the deserializer do the work. Although, as argued by some (@Jimmy), static methods aren't really a gift to programmers. Commented Oct 28, 2014 at 22:30
  • 1
    @Strilanc: that is C#, not Java. Java never allowed cyclic constructors and even if you manipulate the byte code to have such a class file, resurrection wouldn’t work as “An object o is not finalizable until its constructor has invoked the constructor for Object on o and that invocation has completed successfully (that is, without throwing an exception).”. In other words, no super(), no finalize, i.e. no chance for this hack…
    – Holger
    Commented Oct 29, 2014 at 10:49
5

The only difference is that you can still call the constructor inside your class:

public final class UtilityClass {

    public static final UtilityClass Instance = new UtilityClass();

    private UtilityClass () {}

    public static int Foo (int a, int b) {
         return a+b;
    }

}

But since you're the designer of that class, it wouldn't make any sense to break your own code contracts.

In general, most software design books I've ever read, are against using static methods. Unless they are really utility methods: in the sense they will never ever require any state. And even then, it's only a small effort to implement a Singleton pattern such that, when the time would come, you can assign state to it:

public final class UtilityClass {

    public static final UtilityClass Instance = new UtilityClass();

    private UtilityClass () {}

    public int Foo (int a, int b) {
         return a+b;
    }

}

And calling it with UtilityClass.Instance.Foo(2,5);. It would be way harder to perform an introduce state transformation later on in the coding process. Thus static methods are harder to maintain.

The reason why instances are useful is that you can use them in a lot of patterns like Strategy if at one occasion it depends on something what should be done,... By using static methods, one makes the methods less dynamic because Java doesn't support method pointers (for good reasons). Thus non-static methods are more dynamic and useful.

Furthermore some security researchers argue that it is harder to analyze code with static modifiers since they can be accessed from anywhere and the side effects are less predictable (for instance in an automatic security analysis tool): say you have an class that is not fully implemented, then you can still analyze the fields to know which methods it can access and thus analyze the possible side effects (network usage, file IO,...). This can generate a list of possible hazards per class that should be verified. At least if I understood the PhD dissertation of one of my fellow researchers correct. Thus non-static methods allow more modifier analysis.

To conclude: Java was built on the principle of object-oriented programming. This means that the "class world" is used by the compiler, and the "instance world" by the interpreter/runtime. I agree there are a lot of conflicts between the two words. But static methods are in many/some cases a mistake to resolve such conflicts.

1
  • 1
    Humans make mistakes. So even though it wouldn't make sense to break your own code, it happens. Zero risk of error is preferable to tiny risk of error, particularly when you're writing lots of code. Commented Oct 28, 2014 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.