Chapter 2: create and destroy objects. Item1: consider replacing the constructor with the static factory Method

Source: Internet
Author: User
Tags protected constructor

In addition to using the constructor to obtain an instance of the class, you can also use the static factory method ).

The following method converts the basic Boolean Type to a Boolean object reference:

1 public final class Boolean implements Java. io. serializable, 2 comparable <Boolean> 3 {4 // matches 5 public static final Boolean true = new Boolean (true) with the basic type boolean = true ); 6 7 // match with the basic type boolean = False 8 public static final Boolean false = new Boolean (false); 9 10 // if you do not need a new Boolean instance, this method should take precedence over the constructor 11 // because this method has a significant increase in time and space. 12 public static Boolean valueof (Boolean B) {13 return (B? True: false); 14} 15}

Static factory advantages:

1. They have names. : When a class requires multiple constructors with the same signature, the static factory method is used instead of the constructor,Select a name with caution.

2. You do not have to create a new object every time you call them.This allows the immutable class to use pre-built instances, or cache the built instances for reuse to avoid unnecessary repeated objects. The above code illustrates this.

  3. They can return any child type objects of the original return type. Java. util. enumset in jdk1.5 does not have a common constructor and only has a static factory method.

  

/* Create an empty Enum set. Return instances of different types based on the number of enumerated elements. */Public static <E extends Enum <E> enumset <E> noneof (class <E> elementtype) {Enum [] universe = getuniverse (elementtype ); if (universe = NULL) throw new classcastexception (elementtype + "not an Enum"); If (universe. length <= 64) return New regularenumset <> (elementtype, universe); else return New jumboenumset <> (elementtype, universe );}

4. When creating parameterized instances, they make the code more concise.

  

public static <K,V> HashMap<K,V> newInstance() {            return new HashMap<K,V>() ;        }

Disadvantages of static Factory:

1. If the class does not include a public or protected constructor, the class cannot be classified as a quilt.

2. They are no different from other static methods.

Some common names: valueof, of, getinstance, newinstance, GetType, newtype.

 

Chapter 2: create and destroy objects. Item1: consider replacing the constructor with the static factory Method

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.