Effective Java--> (i) Creating and destroying objects

Source: Internet
Author: User
Tags array object constructor include interface reference return string
Create | objects
Effective Java Learning notes the Java language supports four basic types: interface (Interface), Class (Class), array (array), and primitive type (primitive). The first three types are often referred to as reference types (reference type), instances and arrays of classes are objects (object), and the values of primitive types are not objects. A class's members (member) include its domain (field), methods (method), member classes (members Class), and member interface. The prototype of a method (signature) includes its name and the type of all parameters, and the method prototype does not include its return type.

First: Consider using a static factory method instead of a constructor for a class, the most common way to get an instance is to provide the public constructor new one. Alternatively, you can return an instance of a class by providing a static factory method (static factory methods). The following is a static method in singleton mode, which returns a unique instance of the class. public static A getinstance () {return INSTANCE;}

The benefits of Static factory methods: (1) static methods have a name. If the parameters of a constructor do not exactly describe the object being returned, a static factory with the appropriate name can make a class easier to use, and the corresponding client code is easier to read. (2) A static method does not require a new object to be created each time it is invoked. A static factory method can return the same object for a duplicate call. (for example, Singleton mode) (3) A static method can return an object of a subtype of the original return type. (This is often used in interface-based framework structures, and abstract factory patterns, in the Java collections framework Java.util.Collections class, there are a number of useful static methods, which refer to an instance of a hidden implementation class by returning an interface.

The main disadvantage of static factory method: (1) class can not be quilt class if it does not contain public or protected constructors. (Encouraging the use of composite structures rather than inheritance) (2) does not differ from other static methods. This is typically a type conversion operation with [valueof], and the first name [getinstance] method returns an instance. Summary: Static factory methods and public constructors each have their own purpose to understand their strengths in order to design the class structure more rationally.

Fourth: Avoid creating duplicate objects as an extreme negative example, consider the following language name: String s = new string ("sample"); Don ' t do this! Each time the statement is executed, a new instance of string is created (a word that was previously somewhat incomprehensible: a String object is an immutable string, and the StringBuffer object is a mutable string.) This immutable and variable I don't know how to imagine, when I pass a string object as a reference to a method, and then change its value in the method body, but after calling the method, the outside value remains unchanged, which does not conform to the reference rule in Java. Just think of the particularity of the string object: immutable strings) in development work, you often instantiate objects in the method body, and you should consider placing the instantiated object in the method body foreign definition and instantiation when the method is likely to be called frequently and the instantiated object is the same instance. Thus avoiding the creation of duplicate objects. On the other hand, because the constructors of small objects do only a very small amount of work, the creation and recycling of small objects is very inexpensive, especially in modern JVM implementations. This is often a good thing by creating additional objects to make a program clearer, simpler, and more powerful.

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.