Friday, April 1, 2016

Static Vs Transient

Static variables cannot be serialized since the static variables are variable per Class but serialization is intended to serialize the class instance.but still...............
Static variables can be made to serialize if values are assigned there at time of initialization itself as below.
Class Employee implements Serializable{
static String name;
Employee(){
}

Employee(String name){
this.name = name;// initialized at objet creation....... saved suring serialzation
}

}

Class Client{
public static void main(String args[]){

  Employee e = new Employee();
 e.name="Akkhil"; // a varianble named per class hence cannot serialized

Employee e2 = new Employee("akkhil");
}

}

No comments:

Post a Comment