Effective Java Third edition--4. Using private construction methods to perform non-instantiation

Source: Internet
Author: User

Tips
"Effective Java, third Edition" an English version has been published, the second edition of this book presumably many people have read, known as one of the four major Java books, but the second edition of 2009 published, to now nearly 8 years, but with Java 6, 7, 8, and even 9 of the release, the Java language has undergone profound changes.
In the first time here translated into Chinese version. For everyone to learn to share.

4. Using private construction methods to perform non-instantiation

Occasionally you will want to write a class that is just a set of static and static properties. Such classes get a bad reputation, because some people abuse these classes to avoid thinking in an object-oriented way, but they do have a special purpose. They can be used java.lang.Math to java.util.Arrays organize related methods on a numeric or array basis, either in a way or in a manner. They can also be used to group static methods, including factories (entry 1), to implement an object of an interface in the same way java.util.Collections . (Starting with Java 8, you can also put these methods in the interface, if it's your own modification.) Finally, such a class can be used to group methods on the final class, because they cannot be placed in subclasses.

Such a utility class (utility classes) is not designed to be instantiated: an instance is meaningless. However, in the absence of an explicit construction method, the compiler provides a common, non-parametric, default construction method. For the user, the construction method is no different from other construction methods. In the published API, you often see an unconscious class of instances.

attempting to enforce a non-instantiation by creating an abstract class is not feasible. the class can be quilt-like and subclasses can be instantiated. In addition, it misleads the user into thinking that the class was designed for inheritance (entry 19). However, there is an easy way to ensure non-instantiation. A default constructor method is generated only if the class does not contain an explicit construction method, so you can implement non-instantiation of the class by including a private constructor method:

// Noninstantiable utility classpublic class UtilityClass {    // Suppress default constructor for noninstantiability    private UtilityClass() {        throw new AssertionError();    }    ... // Remainder omitted}

Because the explicit construction method is private, it is inaccessible outside of the class. AssertionErrorexceptions are not strictly required, but they provide a guarantee in case the constructor method is called outside of the class. It guarantees that the class will not be instantiated under any circumstances. This idiom is a bit counterintuitive, as if the construction method is designed to not be called. So, as shown earlier, it's wise to add comments.

This habit has a side effect that prevents subclasses of the class. All construction methods must explicitly or implicitly call the parent class construction method, and the subclass does not have an accessible parent class constructor method to invoke.

Effective Java Third edition--4. Using private construction methods to perform non-instantiation

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.