title: How can I initialise a static Map?
body: <p>How would you initialise a static <code>Map</code> in Java?</p>
  
  <p>Method one: static initialiser <br>
  Method two: instance initialiser (anonymous subclass)
  or
  some other method?</p>
  
  <p>What are the pros and cons of each?</p>
  
  <p>Here is an example illustrating the two methods:</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  
  public class Test {
      private static final Map&lt;Integer, String&gt; myMap = new HashMap&lt;&gt;();
      static {
          myMap.put(1, "one");
          myMap.put(2, "two");
      }
  
      private static final Map&lt;Integer, String&gt; myMap2 = new HashMap&lt;&gt;(){
          {
              put(1, "one");
              put(2, "two");
          }
      };
  }
  </code></pre>