Use of Java getinstance ()

Source: Internet
Author: User
Tags instance method

Transferred from: https://www.cnblogs.com/roadone/p/7977544.html

Object Instantiation method, is also more, the most common method is to use the new directly, and this is the most common, if you want to consider other needs, such as single-instance mode, inter-hierarchy calls and so on.

* A good design cannot be achieved directly using new, which requires the use of the indirect use of new, the GetInstance method. This is a representation of a design style, not just a method name.

1. Use of NEW:

As Object _object = new Object (), it is necessary to know that there is a second object, and the second object is often in the current application domain,
That can be called directly.

2. Use of getinstance:
* Called at the beginning of the main function, returns an instantiated object that is static and retains its reference in memory, where there is an area in memory dedicated to static methods and variables.

* Can be used directly, the call returns the same object multiple times.

3. The difference between the two contrasts:
* Most classes (non-abstract classes/interfaces/classes that block constructor) can be new,new either by producing a new instance object, or by declaring an object on the stack, each part of the call
* A new object is used.
* GetInstance is a method of a small number of classes, and their implementation is different.
* getinstance is common in a singleton mode (which guarantees that a class has only one instance and provides a global access point to access it), and is used to generate a unique instance, getinstance is often static.

* (1) The object is obtained by getinstance before use, and does not need to be defined.
* (2) New must be generated to create an object, allocating memory; getinstance () does not have to be created again, it can use an existing reference to you, which is better than new in performance;
* (3) New creation can only be used once, while getinstance () can be used across the stack area, or remotely across regions. So getinstance () is usually created by static instance methods.

* Summary:
* getinstance This method is used in a singleton mode, in order to avoid waste of memory, until it is necessary to instantiate the class to instantiate it, so use getinstance to get the object,
* As for other times, just for simplicity, in order not to allow the program to instantiate the object, do not use the new keyword every time, simply provide a instance method, do not have to execute this class
* Initialize, so do not waste system resources! Single-case mode prevents data conflicts and saves memory space

Use of Java getinstance ()

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.