Java Tip #2: Static factory methods vs. constructors

In: Java

21 Jun 2003

Factory methods are simply methods that instantiate objects. Some factory methods in the Java 2 API that you would likely have used are the getInstance() and valueOf() methods. getInstance() is the conventional instantiation method in singletons, while valueOf() are often type-conversion methods, like in String.valueOf(int i).

Providing a static factory method instead of a public constructor has both advantages and disadvantages, which will be discussed at length in the rest of this entry.

Static factory methods have names
A class can have only a single constructor with a given signature, which is a restriction in cases where a class needs to be able to be instantiated using identical parameter lists. In cases like this, you can replace constructors with static factory methods with more intuitive names that highlight their differences.

Static factory methods can improve performance
This is because static factory methods do not have to actually instantiate new objects each time they are invoked. This is what allows for the Singleton pattern, where a single instance is returned. It also allows instances to be cached within the object in cases where object instantiation is expensive (like for the case of instantiation of fields from a database) or frequently done.

Static factory methods can return a subtype of their return type
This allows for interfaces to be returned by static factory methods, which is best exemplified in the Collections Framework. Needless to say, forcing a client to refer to the returned object by its interface rather than its actual class is a good practice.

Also, the class of the object returned can be private (non-public), so there is a degree of encapsulation where the private class can be modified without impacting clients of the API (possibly for bug-fixing or improvements, or just plain maintainence). The private classes can also vary depending on the parameters to the static factory, so long as they are subtypes of the return type, allowing for greater flexibility.

Classes without public or protected constructors cannot be subclassed
This is an unavoidable fact, so classes designed for inheritance must have at least 1 public constructor to be able to be subclassed.

The verdict? Use static factory methods for a more intuitive API, to improve performance via object-caching, to implement singletons, in interface-based frameworks, and to return objects of private classes. Use constructors in classes designed for inheritance (there shouldn’t be many of these) and for greater visibility in API documentation (since they appear in a separate table in standard Javadocs).

1 Response to Java Tip #2: Static factory methods vs. constructors

Avatar

vlad

June 22nd, 2003 at 2am

This has been discussed ad nauseum elsewhere.