JAVA static method and the understanding of Singleton mode

Source: Internet
Author: User
Tags dateformat

The recent use of sonar to evaluate the quality of the code, found a problem, project in some of the Util class, the static method has been written to suggest the best way to use a single case to correct.

To this end, I thought carefully, found that there is very reasonable. Here's my personal understanding of the static method and the singleton pattern.


The so-called singleton mode, I do not explain too much, recommend the Java design pattern of Zen This book, a good understanding.

Here I talk about two ways of writing:

Once some of the public classes, I am not thinking, used to follow such as the following writing:

public class Dateutil {

Public final static String DateFormat = "YYYY-MM-DD";

Private Dateutil () {

}
public static void Changedateformat () {

}
}
Call the time directly Dateutil.changedateformat ();

Suppose you want this class as a singleton, OK, to add a private constructor directly to the method.

In this way, it seems very good to be able to avoid the user's new object and implement the method invocation.

In fact, the biggest drawback of this approach is that static, as a static method, is loaded into memory when the class is loaded, and it is not recommended to use this location regardless of your usage.


The use of a singleton pattern is a very good way to overcome this problem, such as the following:

public class Dateutil {

Public final String DateFormat = "YYYY-MM-DD";
public static Dateutil instance = NULL;

Private Dateutil () {

}

private static Integer LOCK = 0;

public static Dateutil getinstance () {
Synchronized (LOCK) {
if (instance = = null) {
Instance = new Dateutil ();
}
return instance;
}
}

public void Changedateformat () {

}
}

The same way, it ensures that the user can only get one instance, which conforms to the design idea of the singleton.

At the same time, only when using this instance to invoke the method, the method is added to the memory, when the object is not used, the GC will recycle the method, the efficiency is much higher, of course, I am building a single case is thread-safe.

Introduction here, assuming that the Java memory is interested in reading my previous article, here also recommend a brother, link hair:

1.http://blog.csdn.net/hongshan50/article/details/40583875

2. Assuming that the static method and the normal method occupy Java memory interest, see the following:

Http://blog.sina.com.cn/s/blog_4fe01e630100g775.html




JAVA static method and the understanding of Singleton mode

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.