單例模式(singleton)與static

單例(singleton)模式,本質上,就是給一個靜態變量賦值,而靜態變量在程序的整個內存空間有且僅有1個副本,并且是對外可見的;所以其他的class都可以調用這個副本。
比如下面,在任何地方調用:S10.getInstance() 都會得到同一個S10

方法一

public class S10{

     private SingletonLazy() {

     }
        public static S10 getInstance(){
            return GetS10.instance;
        }
       static class GetS10{
            private static S10 instance=new S10();
        }
    }

方法二

public class S10{
 public static S10 mS10=new S10()
private SingletonLazy() {

    }
 public static S10  getInstance(){
      return mS10
     }
   }

方法三,多線程并發時,容易創建多個不同對象;當然最后一個對象會覆蓋前面的

public class S10{
  public static S10 mS10=null;
  private SingletonLazy() {

   }
   public static S10  getInstance(){
         if(mS10==null){
            mS10=new S10()
          }
        return mS10
      }
    }

下面詳細說一下static及相關的執行順序

先看下這個例子,代碼邏輯:在main里面,調用s1和s2

public class A1 {
    A1(){
        System.out.printf("\n 構造函數");
        System.out.printf("\n 構造函數 "+str1);
    }

    public static String strStatic1="\n靜態成員變量1";
    public  String str1="   000";

    public static void main(String[] str){
        System.out.printf("\n 開始執行main");
//        System.out.printf(strStatic1);
//        System.out.println(str1);
//        s1();
//        new A1().s1();
        s2();

        A1 aaa1=null;
        System.out.printf("\n 第1次new A");
        aaa1=new A1();
        aaa1.s1();

        System.out.printf("\n----------");


        System.out.printf("\n 第2次new 1");
        A1 aaa2=null;
        System.out.printf("\n 第2次new 2");
        aaa2=new A1();
        System.out.printf("\n 第2次new 3");
        aaa2.s1();

        System.out.printf("\n 分割線----------");
        S8 ss=null;
        System.out.println(S8.sss8);
        S9 ss9=null;
    }

    public void s1(){
        System.out.println("\n 開始執行s1");
        String str="\n s1 局部變量";
        System.out.printf(str);
        System.out.printf(str1);

    }
    public static void s2(){
        System.out.println("\n 開始執行s2");
//        String strStatic="";
//        String strStatic1=strStatic1;
        String strStatic="\n s2 局部變量";
        System.out.printf(strStatic);
    }

    static class S8 {
        public static String sss8="\n 靜態變量sss8";
        {

            System.out.println("\n靜態類S8 普通代碼塊");
        }
        static {
            System.out.println("\n靜態類S8 靜態代碼塊");
        }
    }
    class S9 {
        {
            System.out.println("普通類S9 普通代碼塊");
        }
        public String sss8="\n 普通變量sss9";
    }

    {
        System.out.println("\n 普通代碼塊 "+str1);
        str1="  001";
        System.out.println("\n 普通代碼塊 "+str1);
    }

    static {
        System.out.println("\n 靜態代碼塊");
    }

}

//日志 如下

靜態代碼塊

開始執行main
開始執行s2
s2 局部變量

第1次new A
普通代碼塊    000
普通代碼塊   001
構造函數
構造函數   001
開始執行s1
s1 局部變量  001

----------

第2次new 1
第2次new 2
普通代碼塊    000
普通代碼塊   001
構造函數
構造函數   001
第2次new 3
開始執行s1
s1 局部變量  001

分割線----------
靜態類S8 靜態代碼塊
靜態變量sss8

可以看到:
1、靜態代碼塊是在main執行之前執行,并且只執行了這一次。
2、初始化,必然會先執行普通代碼塊,然后才是構造函數
3,new 100次,就會執行100次普通代碼塊,與構造函數
4,定義一個類的變量,并不會執行這個類里面的任何代碼塊
5, 調用內部類的靜態成員變量,會先執行靜態代碼塊
6、定義一個內部類的變量,不會執行其任何代碼塊
7、非靜態內部類不能存在靜態成員變量,和靜態成員方法

我們稍微描述一下,一些相關的變量有什么意思。
一、靜態方法
??一個類里面的靜態方法,不能訪問非靜態成員變量和非靜態成員方法,只能訪問靜態成員變量和靜態成員方法
在static方法中,沒有this這個概念。
1、靜態方法里面不能調用this,如下面S1的寫法是錯誤的,S2才是正確的

public  String str="string 1";
//
public static void s1(){
        String str=this.str;
}
public void s2(){
        String str=this.str;
}

二、靜態變量
靜態變量和非靜態變量的區別是:靜態變量被所有的對象所共享,在內存中只有一個副本,它當且僅當在類初次加載時會被初始化。而非靜態變量是對象所擁有的,在創建對象的時候被初始化,存在多個副本,各個對象擁有的副本互不影響。
靜態成員變量初始化的順序:按照定義的順序,順序初始化
三、靜態代碼塊
全局,不管new多少次,只會初始化1次

四、普通代碼塊
全局,new 多少次,就會初始化多少次

五、默認構造函數
只是一個普通的function,new 一個Class,它會執行在普通代碼塊之后

拓展:

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容