单例模式:

单,是单个的意思,表示一个类只会生成唯一的一个对象

package com.kunlianxi.test;
public class SingletonTest
{
            public static void main(String[] args)
           {
                  Singleton singleton = Singleton.getSingleton();
                  Singleton singleton2 = Singleton.getSingleton();
                  System.out.println(singleton == singleton2);//若相等则表示生成的对象是唯一的
           }
}
class Singleton
{
               private static Singleton singleton = new Singleton();
               private Singleton()  //定义为private的,是为了不让Singleton在外部生成对象,这样                                           才能体现单例模式
               {
  
               }
   public static Singleton getSingleton()
   {
                return singleton;
   }
}