Why do APIs use static factory methods like List.of() instead of constructors?
Static factory methods have names, can return cached or existing objects, can return a subtype, and can hide the implementation class. Constructors can do none of these.
Explain it simply
A constructor is like a counter with a single window that must always give you a brand new item. A named factory method is a shop assistant: "give me a pack of 5", "give me a gift pack". It can also hand you one from stock and decide what is best to give.
Example
// Named, so the intent is clear
LocalDate d = LocalDate.of(2026, 10, 5);
BigInteger p = BigInteger.valueOf(97);
// Can reuse instances
Integer a = Integer.valueOf(100);
Integer b = Integer.valueOf(100);
System.out.println(a == b); // prints: true (small values are cached)
// Can return a hidden subtype
List<String> names = List.of("Asha", "Ravi"); // returns an unmodifiable list implementationCosts: classes with only private constructors cannot be subclassed, and static factories are harder to find in docs than constructors. Common naming: of, from, valueOf, create, getInstance.
What to say in the interview
"Static factory methods have meaningful names, can cache and reuse objects, can return any subtype, and can change the returned class later without breaking callers. The trade-offs are that the class cannot be subclassed if all constructors are private, and they are less discoverable."
Common mistakes
- Using == on Integer values outside the cached range and expecting true.