This post originated from an RSS feed registered with Java Buzz
by Mike Melia.
Original Post: That was weird!
Feed Title: I know the meaning of life, it doesn't help me a bit
Feed URL: http://www.gigantiq.com/blogs/mike/?feed=rss2
Feed Description: Observations on the Java, OSS worlds with an agile perspective. Observation on the .NET world from a learning and agile perspective.
I was trying to construct an object that took a Map as a constructor argument.
e.g.
public Foo(Map bar)
{
this.bar = bar;
}
The client code was instantiating the Foo object like this
Foo foo = new Foo(bar);
and, as a shortcut to populating the Map, I was using an instance initialiser on an anonymous subclass of HashMap
Map bar = new HashMap()
{{
put("key", "value");
}};
All was well until I tried to store my Foo object in flow context (I'm using Spring webflow).
Did anyone else know that that anonymous subclasses of previously Serializable classes are no longer Serializable once you add an instance initialiser?
Anonymous subclasses of Serializable classes can be Serializable (but don't serialise across JVMs).
Classes with instance initialisers can be Serializable.
BUT
Anonymous subclasses of Serializable classes will not be Serializable if the subclass definition adds an anonymous instance initialiser.