代码笔记

import java.util.HashMap;
import java.util.Map;
public class Test{
    private static final Map<Integer, String> myMap = new HashMap<Integer, String>();
    static {
        myMap.put(1, "one");
        myMap.put(2, "two");
    }

    private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>(){
        {
            put(1, "one");
            put(2, "two");
        }
    };
}

public class GsonTest {
    
    {
        System.out.println("log 1");
    }
    
    static {
        System.out.println("Log 2");
    }
    
    public GsonTest() {
        System.out.println("Log 3");
    }

    public static void main(String[] args) throws Exception {
        System.out.println("Log 4");
        GsonTest t1 = new GsonTest();
        System.out.println("Log 5");
    }
}

连接:
https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-initialize-a-static-map.md

import java.util.HashMap;
import java.util.Map;
public class Test{
    private static final Map<Integer, String> myMap = new HashMap<Integer, String>();
    static {
        myMap.put(1, "one");
        myMap.put(2, "two");
    }

    private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>(){
        {
            put(1, "one");
            put(2, "two");
        }
    };
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容