Serializable interface

I have a class that implements the java.io.Serializable interface. Thus, all variables in this class are seralizable. But I want some of the variables not to be serialized. Is it possible?

thanks, Ravi

+4
source share
3 answers

Mark these variables as transient .

eg.

 class A implements Serializable{ int a; transient int b; } 

When object A serialized, transient field b will not be serialized.

+4
source

If you do not 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 it's too extreme, maybe you just want to tweak the serialization a bit? Continue to implement Serializable and implement your own writeObject and readObject .

Here are some examples: http://java.sun.com/developer/technicalArticles/Programming/serialization/

+1
source

You can make the variable transient one and you can see the article below to fully understand the Serializable interface

http://www.codingeek.com/java/io/object-streams-serialization-deserialization-java-example-serializable-interface/

0
source

All Articles