#3880·lombok

[FEATURE] @UtilityClass - throw `AssertionError` on private constructor invocation

Author: bjmiCreated May 9, 2025Updated Sep 1, 2026

If a class is annatotated with @UtilityClass the generated private constructor should prevent both external and internal accidental invocation. The thrown exception provides insurance in case the constructor is accidentally invoked from within the class. At the moment an java.lang.UnsupportedOperationException is thrown which IMHO has cons.

  • UOE is a RuntimeException. But the execution of this sealed constructor is forbidden and should be signaled with an unrecoverable error. E.g. java.util.Objects uses java.lang.AssertionError and java.util.stream.Streams uses java.lang.Error for expressing this.
  • Javadoc of UOE states "This class is a member of the Java Collections Framework." It was originally designed for a different purpose.
  • Item 4: Enforce noninstantiability with a private constructor of Effective Java (Josh Bloch) sample uses an AssertionError.

Therefore please consider throwing an AssertionError to enforce noninstantiability.