Copy Code code as follows:
Package net.kitbox.util;
/**
*
* @author Lldy
*
*/
public class Singleton {
Private Singleton () {
}
private Static Class singletonholder{
private static Singleton instance = new Singleton ();
}
public static void Method () {
Singletonholder.instance._method ();
}
private void _method () {
System.out.println ("Singleton method!");
}
public static void Main (string[] args) {
Singleton.method ();
}
}
This notation utilizes the loading principle of the ClassLoader, where each class is only loaded once, so that a single instance object is generated when its internal static class is loaded, and the process is thread safe.
where method () encapsulates the private method of an internal single Instance object and is used as an external interface, so that it can be invoked as follows
Copy Code code as follows:
Singleton.method ();
Frequent use is more convenient than common singleton.getinstance (). Method ()
Another way to do this is by using enumerations.
The above is the entire content of this article, I hope you can enjoy.