Geek
Geek

Reputation: 27209

Non-transient non-serializable instance field in serializable class

Consider the following code :

public class LIMSGrid extends ClientEventSource implements Focusable, FramingBlockWrapper {

  //cell that is curently in edit mode
  private CellCoord editingCell = null;

  //framing block info
  private FramingBlock framingBlock;   

}

Now ClientEventSource extends a class that implements Serializable interface . The classes CellCoord and FramingBlock are POJOS with a bunch of getters and setters . FindBugs is complaining about the editingCell and framingBlock fields saying :

This Serializable class defines a non-primitive instance field which is neither transient, Serializable, or java.lang.Object, and does not appear to implement the Externalizable interface or the readObject() and writeObject() methods.  Objects of this class will not be deserialized correctly if a non-Serializable object is stored in this field.

Okay so everything is fine except how come it is saying that the instance fields are not "java.lang.Object" . This is totally misleading or I am missing some basics here ?

Upvotes: 4

Views: 27616

Answers (3)

Shivam
Shivam

Reputation: 1

The objects of the classes will not be deserialize correctly if there is any one of the object defined in the class extending the serializable will have non-primitive instance field which is neither transient, Serializable. because if any object of class want to save the its state , it won't be able to do so just because of one non-primitive instance field which is neither transient, Serializable.

Upvotes: -1

JB Nizet
JB Nizet

Reputation: 692121

My guess (but it's only a guess) is that FindBugs doesn't trigger this warning if you reference java.lang.object instances, because it considers that in this case, your class is a generic container, which can hold any kind of object (like a Collection).

In that case, it's the responsibility of the user of the class to make sure that the object stored in the container is serializable if he wants the container to be serializable. (just like an ArrayList is serializable if and only if you store serializable objects inside the list).

Upvotes: 5

Kurt Du Bois
Kurt Du Bois

Reputation: 7665

You should make CellCoord and FramingBlock serializable to avoid that error. If you don't want to serialize them, you should set them as being transient.

Upvotes: 0

Related Questions