public class Hungry {
public static Hungry instance = new Hungry();
private Hungry(){
}
}
class Test{
public static void main(String[] args) {
Hungry instance1 = Hungry.instance;
Hungry instance2 = Hungry.instance;
System.out.println("instance1 == instance2 = " + (instance1 == instance2));
}
}
public class LazyHungry {
static LazyHungry instance = null;
public static LazyHungry getInstance(){
if (instance == null){
instance = new LazyHungry();
}
return instance;
}
}
class Test1{
public static void main(String[] args) {
LazyHungry instance1 = LazyHungry.getInstance();
LazyHungry instance2 = LazyHungry.getInstance();
System.out.println("instance1 == instance2 = " + (instance1 == instance2));
}
}