Java
Why one should prefer static factory methods over constructors in Java?
How named factory methods offer flexible object creation, caching, and clearer Java APIs.
-
Static factory methods have descriptive names (unlike anonymous constructors)
-
They clearly communicate intent (e.g.,
of(),valueOf(),getInstance()) -
They can return cached, pre-existing, or singleton instances
-
They avoid creating unnecessary new objects every time
-
They can return subtypes or completely different implementations
-
They hide implementation classes from clients
-
They enable flyweight, immutable, or canonicalized objects
-
They support multiple creation patterns with the same parameter types
-
They reduce verbosity for parameterized types (especially pre-diamond operator)
-
They form the foundation of modern APIs (List.of, Optional.of, etc.)
-
They allow better naming than overloaded constructors
-
Constructors are still fine for very simple mandatory new-instance cases
-
Use static factories by default unless you specifically need subclassing
-
They make code more flexible, readable, performant, and idiomatic