[Effective Java] [Item 4] Enforce noninstantiability with a private constructor

Опубликовано: 27 Март 2026
на канале: Omkar Shetkar
1,074
11

Creating and Destroying Objects

Item 4: Enforce noninstantiability with a private constructor

Occasionally we want to have a class that is just a grouping of static mehtods and static fields
Used to group related methods on primitive values or arrays Eg., java.lang.Math or java.util.Arrays
Used to group static methods, including factory methods, for objects that implement a particular
interface Eg., java.util.Collections
Used to group methods on a final class, instead of extending the class

Attempting to enforce noninstantiability by making a class abstract does not work.

A class can be made noninstantiable by including a private constructor.
As a side effect, this idiom also prevents the class from being subclassed.