Serializing maps which are initialized in constructors
===

I've just encountered an interesting problem related to Java serialization.

It seems that if my map is defined like this:

    Map<String, String> params = new HashMap<String, String>() {{
      put("param1", "value1");
      put("param2", "value2");
    }};

And I try to serialize it to a file with ObjectOutputStream:

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(outputFile));
    oos.writeObject(params);

...I get java.io.NotSerializableException.

However, if instead I put values to the map the standard way:

    Map<String, String> params = new HashMap<String, String>();
    params.put("param1", "value1");
    params.put("param2", "value2");

...then synchronization work fine.

Can anybody tell me why it happens and what's the difference between these statements? I think they should work the same, but apparently I'm missing something.