What is best practice in Java 8 when I need a bunch of stateless utility methods. Is it right to have an interface that will not be implemented by anyone i.e. public interface Signatures
and public interface Environments
, or is it better to do it the old way - have public final class Signatures
and public final class Environments
with private constructors || enums?

- 730,956
- 141
- 904
- 1,278

- 1,058
- 13
- 20
5 Answers
The main purpose of interfaces is to provide a type and a vocabulary of operations (methods) on that type. They're useful and flexible because they allow multiple implementations, and indeed they are designed to allow implementations that are otherwise unrelated in the class hierarchy.
The question asks,
Is it right to have an interface that will not be implemented by anyone...?
This seems to me to cut against the grain of interfaces. One would have to look around the API to determine that there are no classes that implement this interface, and that there are no producers or consumers of this interface. Somebody might be confused and try to create an implementation of the interface, but of course they wouldn't get very far. While it's possible to have a "utility interface" with all static methods, this isn't as clear as the old unconstructible final class idiom. The advantage of the latter is that the class can enforce that no instances can ever be created.
If you look at the new Java 8 APIs, you'll see that the final class idiom is still used despite the ability to add static methods on interfaces.
Static methods on interfaces have been used for things like factory methods to create instances of those interfaces, or for utility methods that have general applicability across all instances of those interfaces. For example, see the Stream
and Collector
interfaces in java.util.stream
. Each has static factories: Stream.of()
, Stream.empty()
, and Collector.of()
.
But also note that each has companion utility classes StreamSupport
and Collectors
. These are pure utility classes, containing only static methods. Arguably they could be merged into the corresponding interfaces, but that would clutter the interfaces, and would blur the relationship of the methods contained in the classes. For example, StreamSupport
contains a family of related static methods that are all adapters between Spliterator
and Stream
. Merging these into Stream
would probably make things confusing.

- 127,867
- 37
- 205
- 259
-
Thank you for this detailed answer, this is exactly what I was looking for, a logical answer as to why the language now provides this feature and why it still should not be done this way. – maryokhin Aug 02 '14 at 22:40
-
I also can explain why `Collectors` is the separate class (though I'm not a JDK developer). It's usually a good practice to `import static` this class. Were it the part of `Collector` interface, you would get things like `Collector.of` imported as well, but `of` is too short and common word to have it in default namespace. Another thing is that `Collectors` use plenty of private helper methods which should be moved somewhere else if public methods are moved to `Collector` interface. – Tagir Valeev Jun 18 '15 at 09:37
I would use the final class. Communicates to me better that it is a helper class with some utility methods. An interface definition is something I would expect to be implemented and the methods to be there to assist someone implement the interface.

- 1,127
- 9
- 26
-
@kg_SYh Your solution is a good one. It is just "do what we always do" in Java 7 and below. The problems with it are well understood, and most people have a good handle on keeping them from causing too much trouble. Of course, in the context of Java 8, it uses no new features, but maybe that's not really an issue. – Edwin Buck Aug 02 '14 at 21:44
Static methods in interfaces were added with two primary purposes:
In case of poor implementation in subclasses static interface methods can be used to provide checks (e.g. if a value is null).
Avoid using general utility classes (like
Collections
) and calling static methods through their proper interface.
So, it is a very good practice if you intend to share functionality to the corresponding classes.
update:
If you wish to build a pure collection of functions then you may want to use the abstract class with static methods and a private constructor.
-
Yes, they were added to not have a separate util class when you are already implementing an interface, but is it OK to just have an util interface, with static methods that are used, but the interface not being implemented by anyone? – maryokhin Aug 02 '14 at 20:28
-
The issue with util interfaces in my mind is that they open the door to the diamond inheritance problem. In such a case, it's handled better than it ever was before; but, it is still a good problem to avoid. – Edwin Buck Aug 02 '14 at 20:39
In a good object oriented design, there are not many (if any) stateless utility methods.
The best technique I've come to deal with is to use state (Objects) to deal with the function.
So instead of doing
Temperature.farenheitFromCelcius(...);
I do
public class FarenheitFromCelcius implements Function<Celcius, Farenheit> {
public Farenheit apply(Celcius celcius) {
return new Farenheit(5 * celcius.getValue() / 9 + 32);
}
}
This has a few advantages. One being that it can be unloaded from memory much more easily. Another being that you can save on the number of type identifying interfaces, you can pass utility methods between methods, and a final being that you can leverage the Java type hierarchy.
The costs are minimal. Basically you have to alter how the method is applied.
public <T> R convertTemp(T temp, Function<T, R> conversion) {
return conversion.apply(temp);
}
Naturally you'd never write a full method to encapsulate an object oriented function, but I had to show an example...

- 69,361
- 7
- 100
- 138
-
I know that it is against OOP purity to have static methods, but really do not see any sense to create an object for methods like `createSignature(String a, String b){ return "sig: " + a + ", " + b}` – maryokhin Aug 02 '14 at 20:31
-
@maryokhin If you are doing it often, you create the utility once and hold it in a member variable. And if you're creating a Signature, then perhaps a Signature constructor would be more appropriate? But don't take my answer to mean that I do everything OOP, there's a lot of legacy code out there (and it's getting created every day too!) – Edwin Buck Aug 02 '14 at 20:33
-
@EdwinBuck _it can be unloaded from memory much more easily._ Can you explain this please. – ggovan Aug 02 '14 at 21:07
-
1@ggovan Since the utility is now in a class, when the instance of the class is gone, there is a small reduction in memory footprint. Since you are limiting yourself to instance references (instead of class references) you can also unload the Class object by loading it in a non-default ClassLoader (which you will dereference later). With static methods, it is much harder to ensure that all of their usages are unloaded prior to attempting to unload the static utility, so it tends to stick in memory forever. – Edwin Buck Aug 02 '14 at 21:35
-
Thanks for that. Never had so many classes that I've had to worry about unloading them! – ggovan Aug 03 '14 at 01:53
in your case I would go for the final class instead of getting the fatigue that someone might implement or inherent this
. For use-cases where you want a static util interface. I guess we need a final interface for that...
public final class Util {
private Util {
throw new AssertionError("Please don't invoke me");
}
public static someUtilMethod() {}
private static someHelperUtilMethod() {}
}