代码笔记

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

    private static final Map myMap2 = new HashMap(){
        {
            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 myMap = new HashMap();
    static {
        myMap.put(1, "one");
        myMap.put(2, "two");
    }

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

你可能感兴趣的:(代码笔记)