Interview Q&A / Object-Oriented Programming

AdvancedQuick answer1 min readUpdated 3 Oct 2026

Why do APIs use static factory methods like List.of() instead of constructors?

SHORT ANSWER

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

java
// 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 implementation

Costs: 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.

#factory#java#api-design

Related questions

Practise more Object-Oriented Programming questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Object-Oriented Programming quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.