Skip to main content

Very strange Enum serialization problem

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
fra86co
Offline
Joined: 2011-01-18
Points: 0

I've this class hierarchy
//file Type.java
public enum Type{
TypeA,TypeB;
}
//File Message.java
public abstract class Message {
private Type type;
//getter and setter methods
}
//File MessageImpl.java
public class MessageImpl extends Message {
private int x;
//getter and setter methods
}
Now if i try to serialize and deserialize (with ObjectOutputStream and ObjectInputStream open on a socket) a MessageImpl object, i will obtain a MessageImpl instance
with the same 'x' value but with a null value for type field, also if i've specified one.
The strange thing is that if i use this class:
//File MessageImpl2.java
public class MessageImpl2.java{
private Type type;
private int x;
//getter and setter methods
}
the serialization of a MessageImpl2 object works well.
Do you kwow how class hierarchy can affect enum serialization???

Thanks