Quick revision
The short answer
A static factory method is a static method that returns an instance instead of letting callers use a public constructor, for example List.of, Integer.valueOf, Optional.of and Path.of. Unlike constructors, factories have names that say what they create, such as Duration.ofSeconds, can return cached instances as Integer.valueOf does for small values, and can return a subtype or hidden implementation class, as List.of does. The GoF factory method is a related but different idea: a subclass overrides a method to decide which concrete product its superclass creates.
What to remember
In this answer, we saw that a static factory method returns an instance instead of exposing a constructor, as List.of, Integer.valueOf, Optional.of and Path.of do. Factories can have descriptive names like Duration.ofSeconds, return cached instances like Integer.valueOf for small values, and return hidden or varying implementation classes like List.of and EnumSet.noneOf. The GoF factory method is a related idea in which a subclass overrides a method to choose the product, and in modern Java a Supplier often replaces it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A static factory method is a static method that returns an instance instead of letting callers use a public constructor, for example List.of, Integer.valueOf, Optional.of and Path.of. Unlike constructors, factories have names that say what they create, such as Duration.ofSeconds, can return cached instances as Integer.valueOf does for small values, and can return a subtype or hidden implementation class, as List.of does. The GoF factory method is a related but different idea: a subclass overrides a method to decide which concrete product its superclass creates.
Why this question is asked
Look at almost any modern JDK API and objects are created through methods like of, from, valueOf or newInstance rather than through new. This is not a style fashion: static factories let a library change its implementation without breaking callers, save memory through caching, and make code easier to read. Interviewers ask about them to see whether we understand API design and can tell the everyday idiom apart from the GoF pattern with a similar name. In this answer, we will see the limits of constructors, look at JDK examples, go through the three main benefits, and then compare with the GoF factory method.
1The Limits of Constructors
A constructor always has the name of its class, always creates a new object, and always returns exactly that class. Those three facts are usually fine, but they become limits as an API grows. Two ways of creating an object with the same parameter types cannot both be constructors, because they would have the same signature. A constructor cannot return an existing object, even when an identical immutable one already exists. And a constructor cannot decide to return a more specialised class for a particular input.
A static factory method is an ordinary static method that returns an instance of its class or of a subtype. Because it is just a method, none of those three limits apply.
2Static Factories in the JDK
The JDK is full of them. Each example below shows one of the benefits we will discuss.
The naming follows conventions that make factories easy to find: of for a value made from parts, from for a conversion, valueOf for a value of a type, getInstance or newInstance when configuration may decide what comes back. Following the same conventions in our own code helps readers guess the API.
3Benefit 1: Names That Say What They Create
Duration has several factories that all take a long: ofSeconds, ofMillis, ofMinutes, ofDays. As constructors they would all be new Duration(long), which is impossible, and even if it were possible, a reader could not tell what new Duration(90) means. The factory name carries the unit, so Duration.ofSeconds(90) reads like a sentence.
The same applies to our own types. Money.ofMinor(1999, EUR) and Money.of("19.99", EUR) can both exist and are clear, where two constructors with a long and a String would leave readers guessing which is which.
4Benefit 2: Returning Cached Instances
Integer.valueOf keeps a cache of Integer objects for values from -128 to 127, and returns the cached object for those values instead of creating a new one. That is why the identity comparison above is true for 127 and false for 128; the upper bound can be raised with a JVM option, but -128 to 127 is always cached. Autoboxing uses valueOf, so every boxed small number in a program shares a few hundred objects.
This only works because the objects are immutable: sharing an object is safe when nobody can change it. Boolean.valueOf always returns one of two constants, and Optional.empty() returns a single shared instance. A constructor could never do this, because new always creates a new object. It is also why we compare boxed values with equals(), never with ==.
5Benefit 3: Returning a Subtype or a Hidden Class
List.of is declared to return List<E>, and the caller cannot see or depend on the real class. The JDK uses this freedom: it returns a compact class for one or two elements and another class for more, both private to java.util. The implementation has changed between Java releases without breaking anybody, because callers only know the interface.
EnumSet.noneOf is another good example. For an enum with up to 64 constants it returns a RegularEnumSet, which stores the set in a single long, and for larger enums a JumboEnumSet backed by an array. The caller just gets an EnumSet. This flexibility is what makes static factories so valuable in libraries: the class we return today is an implementation detail we can change tomorrow.
6The GoF Factory Method Pattern
The Gang of Four factory method uses the same word for a different mechanism. A superclass contains an algorithm that needs to create an object, and instead of calling a constructor it calls an overridable method; each subclass overrides that method to create its own product. The choice of class happens through inheritance, not through a static method.
| Aspect | Static factory method | GoF factory method |
|---|---|---|
| Form | static method, e.g. List.of | overridable instance method |
| Who chooses the class | The factory's own logic | The subclass |
| Main benefit | Names, caching, hidden classes | Superclass algorithm, varying product |
| Modern alternative | Still the idiom | Pass a Supplier instead |
So both ideas hide the concrete class from the code that uses the object, but in different ways. The static factory is a single method callers use instead of new. The GoF factory method is an extension point inside a class hierarchy. In modern Java, the GoF version is often replaced by passing a Supplier<Writer> to a single exporter class, which gives the same flexibility without subclassing.
7Follow-up questions
What are the downsides of static factories?
A class with only private constructors cannot be subclassed, which is sometimes a benefit, since it encourages composition. Factories are also less visible than constructors in documentation, which is why consistent names like of, from and valueOf matter.
Why should boxed Integers never be compared with ==?
Because valueOf caches only small values, so == happens to be true for numbers from -128 to 127 and false for larger ones. Code that works in tests with small ids can fail in production with large ones. equals() compares values and is always correct.
Is Optional.of different from Optional.ofNullable?
Yes, and the names show it. Optional.of(value) throws a NullPointerException if the value is null, while Optional.ofNullable(value) returns the empty Optional for null. Two constructors with the same parameter could not express that difference; two named factories can.
8Conclusion
In this answer, we saw that a static factory method returns an instance instead of exposing a constructor, as List.of, Integer.valueOf, Optional.of and Path.of do. Factories can have descriptive names like Duration.ofSeconds, return cached instances like Integer.valueOf for small values, and return hidden or varying implementation classes like List.of and EnumSet.noneOf. The GoF factory method is a related idea in which a subclass overrides a method to choose the product, and in modern Java a Supplier often replaces it.
Open with
A static factory method is a static method that returns an instance, used instead of a public constructor. The JDK uses them everywhere: List.of, Integer.valueOf, Optional.of, Path.of, Duration.ofSeconds.
Then, if they want more
They have three main advantages over constructors. First, names: Duration has ofSeconds, ofMillis and ofMinutes, which all take a long, so they could not even be separate constructors, and the name tells the reader the unit.
Second, they can return cached instances, since they are not forced to create a new object; Integer.valueOf caches values from minus 128 to 127, which is why comparing boxed integers with double equals works for small numbers and fails for large ones, and why we use equals.
Third, they can return a subtype or a hidden implementation: List.of returns private classes like List12 or ListN, and EnumSet.noneOf picks a long-based or array-based set depending on the enum size, so the implementation can change between Java versions without breaking callers.
The GoF factory method is related but different: a superclass algorithm calls an overridable method, and each subclass overrides it to decide which concrete product to create, so the choice happens through inheritance. In modern Java I would often replace that with a Supplier passed in.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the factory method idea and static factory methods in Java. Give JDK examples...": https://scrivio-interview-library.netlify.app/java/creational-patterns/b0af8ae14d/