[Effective Java Reading notes] Chapter II Creating and destroying objects the first article

Source: Internet
Author: User

Chapter II Creating and Destroying objects

The first one uses the static factory method instead of the constructor, because:

  1. Static factory methods can have different names, that is, the constructor can only be distinguished by different parameters, and static factories can express different purposes in their names.
  2. The static factory method creates a new object without each invocation (in fact because it is static, so it can only be used as static, so it is created early and does not need to be created repeatedly). ), such as boolean.valueof (Boolean) in the book
     1  public  static  final  Boolean TRUE = new  Boolean (true  );  2   Public  static  Boolean valueOf (boolean   b) { 4  return  (b  True:false);  5   True and false here are well-established instances that do not need to be created every time the method is called  6 } 
  3. A static factory method can return an object of any primitive type subtype,?????
  4. Static factory methods, which make the code more concise when creating instances of parameterized types. Like what's in the book.
    // Suppose HashMap provides this static factory:  Public static <K,V> hashmap<k,v> newinstance () {    returnnew hashmap< K,v>();} // Then: map<string,list<string>> m = hashmap.newinstance (); // more concise than the following: New Hashmap<string,list<string>> ();

[Effective Java Reading notes] Chapter II Creating and destroying objects the first article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.