Hi,
I have a class that implements java.io.Serializable interface.So all the variables in that class is serilaizable. But i want to make some of the variables are should not serializable. Is it possible?
thanks, Ravi
Hi,
I have a class that implements java.io.Serializable interface.So all the variables in that class is serilaizable. But i want to make some of the variables are should not serializable. Is it possible?
thanks, Ravi
Mark those variables as transient.
e.g.
class A implements Serializable{
int a;
transient int b;
}
When an object of A is serialized, the transient field b will not be serialized.
If you don't want to use transient (why not?), you can implement Externalizable and implement your own protocol:
public class Spaceship implements Externalizable {
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// ...
}
public void writeExternal(ObjectOutput out) throws IOException {
// ...
}
}
If that's too extreme, maybe you just want to customize the serialization a bit? Keep implementing Serializable and implement your own writeObject and readObject methods.
Here's a bunch of examples: http://java.sun.com/developer/technicalArticles/Programming/serialization/