A Java static initializer block example (static initialization of a HashMap)

I just ran across a nice example of a Java static initializer block example in an Android book, and thought it might be helpful to share that example here.

First, here's the static initialization code:

private final static Map<Integer, String> MESSAGES;

static {
    MESSAGES = new HashMap<Integer, String>();
    MESSAGES.put(R.id.non_ghost, "I ain't 'fraid of no ghost!");
    MESSAGES.put(R.id.ghost, "Boo!");

As you can see, the author declares a reference to a static field named MESSAGES, which is a Java Map. He then assigns an object to that reference, creating it as a HashMap, and then add elements to that object in the static block created after the field is declared.

I don't have the book with me at the moment, but I think this example comes from a very good book named Beginning Android 3.

I just wanted to share that static initialization example here today, but for more information on static initializers, see this brief tutorial in Oracle's Java tutorial trail.