Pros:
- Has names --> Improve code readability
// Static factory method BigInteger.probablePrime // Regular constructor --> probably return a prime BigInteger(int, int, Random)
- Avoid multiple constructors --> easier for end users to
- Doesn't require to create a new object.
- Return an object of any subtype of
- Simplify constructor
- Provide clean code.
- Make clear that the class is a singleton
- Provide flexibility to change its API later. ? (Item 27)
- Some utilitiy classes were not designed to be instaniated. (e.g.
java.util.Collections
). - However, these can be subclassed and the subcleass instaniated --> potentially mislead user into thinkings that class was designed for inheritance.
- Simple Idiom to ensure noninstantiability - Private Constructor
public class UtilityClass {
// Suppress default constructor for noninstaniability
private UtilityClass() { throw new AssertionError();}
}
- It is appropriate to resuse a single object instead of creating a new functionally equilvalent object each time is needed.
- Reuse can be both faster and more stylish.
- An object can be reused if it is immutable.
- To avoid memory leak.
public class Stack {
private Object[] elements;
private int size;
public void push(Object e) {
elements[size++] = e;
}
public Object pop() {
// return elements[--size]; // <---- leak here due to osolete references.
// Better solution - null out array
Object result = elements[--size];
elements[size] = null; // Avoid obsolete reference
return result;
}
//...
}
Obsolete reference
is a reference that will never be dereferenced again.