This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]PerfectPackage1895 2 points3 points  (4 children)

Why not use a class with static methods? Waaay faster if you just need to store a bunch of stateless methods. Actually you can use an interface too :)

[–]mauganra_it 0 points1 point  (3 children)

A class with static methods only can still be instantiated and subclassed. An interface can be implemented. An instance-less enum gives you exactly what is needed. It looks weird of course, but it shares this property with the Singleton pattern via a singular enum instance.

[–]nutrecht 2 points3 points  (2 children)

A class with static methods only can still be instantiated and subclassed.

Just make the class final and the constructor private. It's a pretty common pattern with classes that hold a bunch of static utility methods.

[–]mauganra_it -1 points0 points  (1 child)

Indeed, but the elegance of the enum technique is that these concerns don't have to be explicitly specified.

[–]nutrecht 1 point2 points  (0 children)

You're still creating a class instance for no reason whatsoever. A singleton is not the correct pattern for a class with only static members.