How to better Create a Java object (1)

Source: Internet
Author: User

In the previous article, we realized that in daily coding, the new method is often used easily, but in some scenarios, object instances do not need to be created repeatedly, such as task Manager. If you still use new to create objects constantly, it is a waste of resources. Let's talk about the singleton pattern.

Singleton, where only a single instance exists. Below we are referring to the single application under single example.

To implement a singleton, new cannot be opened to the outside world, so the construction method needs to be set to private. At the same time for outside access, you need to provide a way to get an instance. The code is as follows:

  

public class Demo {Private Final Demo demo = new Demo ();p rivate demo () {}public demo getinstance () {return demo;}}

The member properties of the demo are instantiated after the demo class is loaded. This approach is called a hungry man. The disadvantage is obvious: if the demo has not been used, the memory allocated for the demo has been wasted. Can we instantiate it when we need it?

 

public class Demo {private demo Demo;private demo () {}public demo getinstance () {if (demo==null) {demo = new demo ();} return demo;}}

In single-threaded cases, the demo type is really a single instance (regardless of the demo object produced by serialization). With so many threads, it is still possible to create multiple instances. How to modify it?

public class Demo {private demo Demo;private demo () {}public demo getinstance () {if (demo==null) {synchronized (this) {demo = New Demo ();}} return demo;}}

We have been able to implement a single case of multithreading in a synchronous way. Is there any other better way to implement it?

public class Demo {private demo () {}private static class Demoinstance{public static Final Demo demo = new demo ();} Public Demo getinstance () {return demoinstance.demo;}}

We can choose different way of creation according to our own preference. The above is purely personal understanding, if there are errors, please correct.

How to better Create a Java object (1)

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.