I was reading and implementing scenario's through enum. I figured out we can create an enum
without any instance. What is the practical use of such an Enum
? Secondly Enum
can also implement
an interface, but obviously can't extend a class as it already extends class Enum
. What are practical advantages of creating an Enum
without instances?
Ben

- 4,561
- 7
- 37
- 68
-
Oh that ways. But many interviewers ask this question. If there is none, why would Java people allow it? Somewhere may be there is some use, we are not aware about it. – benz Jul 22 '13 at 16:59
-
See also: http://stackoverflow.com/a/14900526/829571 – assylias Jul 22 '13 at 17:03
3 Answers
Zero-member enums are actually a utility class idiom used by a certain segment of the Java community (most notably, Peter Lawrey). They are the most concise, and arguably the cleanest way to guarantee that the class may not be instantiated or subclassed.
Naturally, you will not have any instance methods in such an enum; only static ones.

- 195,646
- 29
- 319
- 436
-
4Can you give an example of where something like this is done? I can think of many where a regular class is used (e.g. `java.lang.Math`), but I don't think I have ever seen this. – arshajii Jul 22 '13 at 17:07
-
3But it then appears in the `enum` section of the javadoc, which is not necessarily where you would look for it. A final (for clarity) class + private constructor is IMO the standard idiom for such a use case (although it is functionally equivalent). That is what is typically done in the JDK (including 8). – assylias Jul 22 '13 at 17:09
-
1
-
"well-known": can you provide an example/reference to validate this claim? I've never seen an enum used this way. – DannyMo Jul 22 '13 at 17:13
-
1@damo I could be biased, I admit. I read [Peter Lawrey's blog](http://vanillajava.blogspot.com/2010/08/two-uses-for-enum-most-people-forget.html) on the subject a long time ago so this idiom has grown on me. – Marko Topolnik Jul 22 '13 at 17:18
-
I will also admit that, i saw this concept somewhere on blog that a enum without instance can be used as a Utility class. But still i want to find out what kind of utility class. For such a utility class ain't a normal static class enough. Why would someone go with Enum in this case? – benz Jul 22 '13 at 17:21
-
1@benz Because it is more concise, which is not at all just about character count: just by seeing the one word `enum` you can immediately rest assured that this class satisfies all requirements for utility classes: no subclassing, no instantiation. It is about *mental load* placed on the programmer. When each piece of project code is blessed with such grace, the overall effect is way beyond subtle. – Marko Topolnik Jul 22 '13 at 17:24
-
That's correct, i agree with your concept for sure. On the more logical basis it seems a perfect solution. – benz Jul 22 '13 at 17:25
-
So Lawrey showed two ways to abuse `enum`:) Who cares if someone subclasses `Math` or instantiates a `Math` object. There are utility classes that can be subclassed and insntatied and they never caused any problem. For example: `Beans, Modifier, URLDecoder`. (but not URLEncoder, LOL) – ZhongYu Jul 22 '13 at 18:50
-
1I remember when I've suggested to use enums as utility classes, it was so much hate... Do people change minds? http://stackoverflow.com/questions/4520216/how-to-add-test-coverage-to-a-private-constructor/9036270#9036270 – kan Jul 22 '13 at 19:24
-
@kan Yes, people many times can't distinguish their religious beliefs from objective truth. Once upon a time someone probably argued "How can you possibly make a class which cannot be instantiated? A class is a template for making objects and any other use of it is a *travesty* and a clear code smell". Well, times have changed for those people. – Marko Topolnik Jul 22 '13 at 19:34
-
Marko: neat idea on the enum, and glad I learned abou it here so I will recognize it in future code. But I don't see any _problem_ with instantiating or even subclassing a class that is all statics, like Math. Can you provide an example? – user949300 Jul 23 '13 at 05:43
-
@user949300 There isn't any problem; it's about conveying intentions. – Marko Topolnik Jul 23 '13 at 08:09
Enum
are reference types like class
or interfaces
. This means you can
well adapt the principle , "program to interface and not implementation".
Members of an enum
is not necessarily fixed till the end of universe. You can add new members without breaking binary comparability. So I can imagine that an enum
is created to model some concept which has no instances at the present time but may have some added later.
Is it also possible that an enum
is created for a concept that we know definitely will never have any instances? For example enum NegativeNaturalNumber{}
? Could it serve any purpose in a program? Who knows, we cannot say with certainty.

- 19,446
- 5
- 33
- 61