Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between java enum with no values and utility class with private constructor

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?

like image 610
Craig Gidney Avatar asked Oct 28 '14 21:10

Craig Gidney


People also ask

Can enum have private constructor Java?

Now, you know that Enum can have a constructor in Java that can be used to pass data to Enum constants, just like we passed action here. Though Enum constructor cannot be protected or public, it can either have private or default modifier only.

Can enum have private constructor?

We need the enum constructor to be private because enums define a finite set of values (SMALL, MEDIUM, LARGE). If the constructor was public, people could potentially create more value. (for example, invalid/undeclared values such as ANYSIZE, YOURSIZE, etc.). Enum in Java contains fixed constant values.

What's the difference between an enum constructor and a regular class constructor?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Are enum constructors private by default?

Enum cannot have a default no-argument constructor and all its constructors (if any) are private; therefore, it is redundant to specify them explicitly. Note that the enum constructor classes are implicit and do not require new keyword.


2 Answers

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.

like image 199
Mike Baranczak Avatar answered Oct 22 '22 05:10

Mike Baranczak


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.

like image 41
Andy Thomas Avatar answered Oct 22 '22 04:10

Andy Thomas