Functions of private constructors

Source: Internet
Author: User
For example, a class such as publicclassDiskUtil {privateDiskUtil () {} publicstaticFUNC () {}} usually provides some static functions to generate instances of this class, as long as this class has public attributes and methods, one party that gets the instance reference can still call it. One example is singleton. You can only call DiskU for external calls.

For example

Public class DiskUtil {private DiskUtil () {} public static FUNC (){}}

Such a class usually provides some static functions to generate the instance of this class. as long as this class has public attributes and methods, the party that gets the instance reference can still call it, one example is singleton.


The external call can only call the static function Func of DiskUtil, but not A a = new A (). The private constructor can only be called inside the function and cannot be instantiated outside the function, therefore, private constructors can prevent this class from being instantiated externally.


Common applications are Tool Type and Singleton mode.

Package test. reflect; public class Singleton {private static Singleton s = null; private Singleton () {} public static Singleton getInstance () {if (s = null) {synchronized (Singleton. class) {if (s = null) {s = new Singleton () ;}} return s ;}}


Related 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.