Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When serializing an object, how can I prevent serialization of particular members?

I have a class that implements Serializable. When I serialize members of that class, there are certain variables/methods that I don't to want included in the serialized representation.

Consider a Name class that is Serializable:

class Name implements Serializable {
    private String firstName;
    private String middleName;
    private String lastName;

    ...
}

Suppose I want the serialized form of each instance to exclude the middleName property.

Is there any way I can exclude particular properties and methods from being included in the serialized representation of the object?

like image 703
Mohit Avatar asked Sep 11 '14 12:09

Mohit


1 Answers

transient keyword is used before variables whom you dont want to be serialized

eg.

private transient yourvariable;

and also transient cant be used with methods its for variables only

like image 97
kirti Avatar answered Sep 19 '22 13:09

kirti