Java USES static factories instead of constructors for methods and advantages
- 2020-04-01 03:00:51
- OfStack
Form 1.
public static Boolean valueOf(boolean b) {
return b ? Boolean.TRUE : Boolean.FALSE;
}
2. Advantages:
You can have a name
Instead of creating a new object, you can return an existing object
Objects of subclass type can be returned (example: java.util.collections)
Shorten the parameterized code (example: new HashMap< String, List< String> > () to hashmap.newinstance ())
3. Disadvantages: cannot be subclassed, not recognized by IDE tools.
4. Common name: valueOf/of; GetInstance (Singleton); NewInstance; GetType (for different classes); newType