Java final abstract class Java final abstract class java java

Java final abstract class


You can't get much simpler than using an enum with no instances.

public enum MyLib {;   public static void myHelperMethod() { }}

This class is final, with explicitly no instances and a private constructor.

This is detected by the compiler rather than as a runtime error. (unlike throwing an exception)


Reference: Effective Java 2nd Edition Item 4 "Enforce noninstantiability with a private constructor"

public final class MyClass { //final not required but clearly states intention    //private default constructor ==> can't be instantiated    //side effect: class is final because it can't be subclassed:    //super() can't be called from subclasses    private MyClass() {        throw new AssertionError()    }    //...    public static void doSomething() {}}


No, what you should do is create a private empty constructor that throws an exception in it's body. Java is an Object-Oriented language and a class that is never to be instantiated is itself a work-around! :)

final class MyLib{    private MyLib(){        throw new IllegalStateException( "Do not instantiate this class." );    }    // static methods go here}