Sub topic 7
Static Factory Methods
What a static factory method is
This chapter assumes that you can define classes, write constructors and methods, create objects with new, and use basic inheritance and overriding.
A static factory method is a static method that creates and returns an object. It is called on the class rather than on an existing object:
Temperature t = Temperature.ofFahrenheit(68);The method is called a factory because it produces an instance. It is not the same as the Factory Method design pattern. A static factory method is simply a named class method that controls object creation.
A named way to create objects
Constructors have the same name as their class. If a class has several constructors, the caller must understand what each parameter means. Static factory methods can give each creation choice a clear name.
final class Temperature {
private final double celsius;
private Temperature(double celsius) {
this.celsius = celsius;
}
public static Temperature ofCelsius(double celsius) {
return new Temperature(celsius);
}
public static Temperature ofFahrenheit(double fahrenheit) {
double celsius = (fahrenheit - 32) * 5 / 9;
return new Temperature(celsius);
}
public static Temperature roomTemperature() {
return new Temperature(21.0);
}
public double celsius() {
return celsius;
}
}
public class TemperatureDemo {
public static void main(String[] args) {
Temperature fromFahrenheit = Temperature.ofFahrenheit(68);
Temperature defaultTemperature = Temperature.roomTemperature();
System.out.println(fromFahrenheit.celsius());
System.out.println(defaultTemperature.celsius());
}
}The constructor is private, so callers cannot use new Temperature(...) directly. They must use one of the named methods. This lets the class check, convert, or otherwise prepare values before creating the object.
Why use a static factory method?
Static factory methods provide several useful choices.
| Benefit | Example |
|---|---|
| A descriptive name | `ofFahrenheit(68)` explains the input unit |
| Controlled creation | The class can reject invalid values |
| Reuse of instances | The method can return a cached object |
| Flexible return type | The method can return a subtype or interface |
| Future changes | The implementation can change without changing callers |
A factory can return an existing object instead of creating a new one. This is useful for immutable objects, which cannot change after construction:
public final class AccessLevel {
private static final AccessLevel GUEST = new AccessLevel("guest");
private final String name;
private AccessLevel(String name) {
this.name = name;
}
public static AccessLevel guest() {
return GUEST;
}
public String name() {
return name;
}
}Every call to AccessLevel.guest() returns the same object. This behavior is part of the method's contract, so use it only when sharing the instance is safe.
Common naming conventions
Java libraries commonly use names that describe how the value is created:
of: creates an instance from one or more values, such asList.of("a", "b").from: converts another type, such asDate.from(instant).valueOf: returns an equivalent value, such asInteger.valueOf(42).parse: reads text and converts it, such asLocalDate.parse("2026-11-26").createornewInstance: emphasizes that a new instance is made.
Choose names that make units, conversions, and validation visible.
Trade-offs and common errors
A factory method can hide the concrete class returned. This is useful when callers should depend on an interface, but it can make debugging or documentation less obvious. A private constructor also prevents subclassing when the class is final, so decide whether inheritance is part of the design.
Do not use a static factory merely to rename a constructor without a reason. A public constructor is often simpler when there is only one straightforward creation path. Also remember that a static factory method is not an instance method and cannot use this.
Exercise
Why might Temperature.ofFahrenheit(68) be clearer than new Temperature(20.0)?
Show answer
The factory method names the input unit and performs the conversion, so the caller does not need to know the internal representation.