class Test{
    public static void main(String[] args) {
        singleton s1=singleton.getinstance();
        singleton s2=singleton.getinstance();
        System.out.println(s1);
        System.out.println(s2);

    }
}
class singleton{

 private final static singleton per=new singleton();
    private singleton (){};
    public  static singleton getinstance(){
        return per;
    }
}

运行结果:

Java饿汉单例模式

 

由于单例模式产生的对象是唯一的,所以用来接收实例对象地址的s1,s2的地址也必然是一样的

 

相关文章:

  • 2022-02-21
  • 2022-12-23
  • 2021-10-28
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2021-05-02
  • 2022-12-23
  • 2021-11-25
  • 2022-01-05
  • 2021-04-11
相关资源
相似解决方案