Replacing Utility Classes With Interfaces And Java 8 📎
Utility classes act a a bag of related static methods with a private constructor:
package java.lang;
public final class Math {
/**
* Don't let anyone instantiate this class.
*/
private Math() {}
public static double sin(double a) { /**/}
Java 8 introduces a leaner alternative -- the "utility interface" and makes the private anti-constructor superfluous:
public interface Math {
public static double sin(double a) { /**/}
}
See e.g. Contexts.java from the nano project.
See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.