public class Animal {
private static Animal animal = new Animal();
public static Animal newInstance() {
return animal;
}
}
public class Main {
public static void main(String[] args) {
Animal animal =Animal.newInstance();
System.out.println(animal.hashCode());
}
}
注:
单例模式就是为了解决要new出来好多好多个对象来,使代码简单化。