My Misconception about Serializable (maybe you have it too...)

Hi all,

I was shocked, SHOCKED!, to find out that there is still things to learn from reading the Java API docs … :smile:
I had a misconception about what is required to be Serializable and just in case you have that misconception too, here it comes:

I’m just working on some stuff where I need something (SCIFIOCell, if you must know) to be Serializable.

Now SCIFIOCell used to extend AbstractCell which, at the time, we let implement Serializable for exactly that reason. We also removed final modifiers from some fields and added a no-arg constructor:

As it turns out, that was completely unnecessary. The “rules” of serialization say that the first non-serializable parent class in the hierarchy has an accessible no-arg constructor. In our case that is Object. So simply writing AbstractCell implements Serializable would have sufficed (and this is what I will do this time around…)

Here is an example. This works just fine:

public class SerializeExample
	public static class A implements Serializable
		private static final long serialVersionUID = 1L;

		private final String name;

		public A( final String name )
		{ = name;

	public static void main( final String[] args ) throws IOException, ClassNotFoundException
		final String filename = "/Users/pietzsch/Desktop/SERIALIZED";

		final ObjectOutputStream o = new ObjectOutputStream( new FileOutputStream( filename ) );
		o.writeObject( new A( "Hello, I'm an A" ) );
		o.writeObject( new A( "Hello, I'm a different A" ) );

		final ObjectInputStream i = new ObjectInputStream( new FileInputStream( filename ) );
		System.out.println( ( ( A ) i.readObject() ).name );
		System.out.println( ( ( A ) i.readObject() ).name );

best regards,