First Kind
1 Packagecom.atguigu.javase;2 Importjava.io.IOException;3 4 /**5 * @author_al0n4k6 * @version1.07 * @time September 2, 2015 PM 6:12:528 */9 Public classSingleton_hungermode {//Singleton mode-Hunger typeTen PrivateSingleton_hungermode () {}//set the constructor to private so that the new object cannot be One A Private StaticSingleton_hungermode Singleton =NewSingleton_hungermode (); - //can only be created in this class, where static is added because the method gets the object, because the object cannot be created, - //Therefore, you can only access the object by means of the class name. Then static methods cannot access non-static methods, so here also write the static the //Add private because there is no need to access him in other classes. - Public StaticSingleton_hungermode Getsingleton () { - //returns the created object, plus static, to be invoked in other classes with the class name. - //access permission to add public or you cannot invoke the method in the same way as the class name in another class + returnSingleton; - } + } A classsingleton_hungermodetest{ at - Public Static voidMain (string[] args)throwsIOException { -Singleton_hungermode s =Singleton_hungermode.getsingleton (); - //The static method is accessed through the class name to break the limit of the object that cannot be created in another class. -Singleton_hungermode S1 =Singleton_hungermode.getsingleton (); - in if(s = =S1) { -System.out.println ("is the same object"); to } + ElseSystem.out.println ("Not the same object"); - the * $ }Panax Notoginseng}
The second Kind
1 Packagecom.atguigu.javase;2 3 /**4 * @author_al0n4k5 * @version1.06 * @time September 2, 2015 PM 6:12:477 */8 Public classSingleton_idlermode {//Singleton mode-Lazy type9 PrivateSingleton_idlermode () {}//set the constructor to private to prevent objects from being created in the outer classTen Private StaticSingleton_idlermode Singleton =NULL; One //set a reference to NULL so that only let it be called inside the class. Because objects cannot be created, we need static decoration with static, then A //the name of the class to access. Write a method below that gets the object reference, adding static to call the method in another class to get a reference to the object . - //so we're going to add static here too . - the //Public is written here because you want to call this method outside the class, get the object reference, and add static because you cannot create an object to invoke it in the way of the class name. - //because you want to return a reference to this class, the return value is written as Singleton - Public StaticSingleton_idlermode Getsingleton () { - //If the reference is empty we are going to create the object and then return the reference, and because it is a singleton we can only call once. + //when this method is called, Singleton is not empty, so add a judgment. Only if this condition is met, the reference to the object is created here. - if(Singleton = =NULL) { +Singleton =NewSingleton_idlermode (); A } at returnSingleton; - } - - } - - //Write a test class test, create a 2 object is not to point to an address, if it is a description is an object. in classSingleton_idlermodetest { - Public Static voidMain (string[] args) { toSingleton_idlermode Singleton1 =Singleton_idlermode.getsingleton (); +Singleton_idlermode Singleton2 =Singleton_idlermode.getsingleton (); - //the 2 referenced address equality description is an object, then it conforms to the singleton mode the if(Singleton1 = =Singleton2) { *System.out.println ("is an object"); $}ElseSystem.out.println ("Not an object");Panax Notoginseng } -}
Java Two single-instance mode