Creating and destroying objects
Article 1 is configured by considering instead of a static factory method
- Static factory method has a name, it can accurately describe the object being returned.
- You do not have to call creates a new object each time.
- You can return to the original return type of any child class object.
- More concise when creating a parameterized type instances, such as when call the constructor HashMap, use
Map < String,List < String > m = HashMap.newInstance()
, andMap < String,List < String > m > = new HashMap < String,List < String > >()
;
practice
JDK
Source provides static methods:Collections.emptyList();
(Note that the return of the List is not java.util.ArrayList but java.util.Collections.EmptyList inner class)google
Theguava
package offersMaps.newHashMap()
andLists.newArrayList()
static methods create objects (returns java.util.ArrayList # ArrayList () and java.util.HashMap # HashMap ())
Reference:
"as Effective the Java" - reading summary