java - How affect class who have static attributes -


i want know how deserilize class contain staic attributes file because when create instance project , can't affect global class
my code : ( deserialize method doesn't work )

public class project implements serializable{      private static  string name;     private static  string site;     private static  table table;   public static  string getname() {     return project.name; }  public static   void setname(string name) {     project.name = name; }  public static  string getsite() {     return project.site; }  public static  void setsite(string site) {     project.site = site; }  public static  table gettable() {     return project.table; }  public static  void settable(table table) {     project.table = table; }  // serialize public static  boolean  serialize(string path){        try{          fileoutputstream fout = new fileoutputstream(path);         crypto.encrypt(project.class, fout);          return true;        }catch(exception ex){            return false;        }    }     public static boolean deserialze(string path){         try{            fileinputstream fin = new fileinputstream(path);           project project = (project) crypto.decrypt(fin);// decrypt file           project.name = project.getname();           project.site = project.getsite();           project.table = project.gettable();           return true;        }catch(exception ex){            return false;         }  

serialization works objects — instances of classes. static fields aren't part of instance. serialization doesn't touch them.

you're not serializing instance of project class, though. you're serializing class object itself, instance of java.lang.class class. can see why you'd think might store static fields, doesn't: class object reflection, getting information about class. doesn't hold class's data; static fields in class not fields of project.class object. afaik, serializing class object not useful thing do.

your fields shouldn't static anyway, because hold information should different each project. right now, have single name that's shared across all projects, , single site, , single table. run new project() fifty times , have fifty distinct objects, there's no way make them different each other. program has no way represent 2 projects different names.

my advice: take out static keywords, change static field references (e.g. project.name) instance field references (e.g. this.name), create instance of class (e.g. project project = new project()) , set fields, , serialize that.


Comments