Question

I've gone through and programmed all my domain level logic to interfaces. Now I find that when I want to put data in a bean, the bean doesn't work because the Collection interfaces (Collection, List, Set, etc) do not implement Serializable.

Do I need to refactor all my code to use concrete types or is there a better course of action here?

Was it helpful?

Solution

If your class implements Serializable and all of its members are serializable, then the object can be serialized correctly. Example:

public class Person implements Serializable {
    private String name;
    private Collection<Integer> luckyNumbers = new ArrayList<Integer>();
}

As long as luckyNumbers's instance is serializable (such as ArrayList), and its members are serializable (in this case Integers) , then the object will serialize.

OTHER TIPS

Serializable is a "marker" interface. It is not suitable to be used a reference's type. Live with a bit of dynamic typing (though there is nothing to stop you using an external static type checker).

You could jump through hoops with weird parameterised generic methods, but it would be extremely ugly and the Java libraries don't do that so you'd be hosed anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top