开发者

serializable interface

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 seri开发者_Python百科alizable. 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/


You can make the variable a transient one and can look the article below for completely understanding the Serializable Interface

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜