单例模式饿汉式
程序员文章站
2022-05-23 19:42:00
...
单例模式
饿汉式:类一加载就创建对象
public class Student {
private Student() {
}
//静态随着类的加载而加载,所以叫做饿汉式
private static Student s = new Student();
public static Student GetStudent() {
return s;
}
}
public class StudentDemo {
public static void main(String[] args) {
Student s1=Student.GetStudent();
Student s2=Student.GetStudent();
System.out.println(s1==s2);//true
}
}
上一篇: 抽象类