---------- Forwarded message ----------
From: Adam Smith <[email protected]>
Date: Wed, Apr 22, 2009 at 4:12 PM
Subject: Re: [java programming] Re: Help.......LAB-1043......Serialization
To: [email protected]


Thanks for your help.  Once I did all this, it still didnt change the
value.  Here's an example of the school class's code.

public class School implements Serializable{

     private String nameOfSchool;
     transient private int yearStarted;

    public String getNameOfSchool() {
        return nameOfSchool;
    }

    public int getYearStarted() {
        return yearStarted;
    }

    public void setNameOfSchool(String nameOfSchool) {
        this.nameOfSchool = nameOfSchool;
    }

    public void setYearStarted(int yearStarted) {
        this.yearStarted = yearStarted;
    }

     public School(){

     }

     public static School getSchool(String nameOfSchool, int yearStarted){
         School temp = new School();
         temp.setNameOfSchool(nameOfSchool);
         temp.setYearStarted(yearStarted);
         return temp;
     }

     @Override
     public String toString(){
         StringBuffer myBuffer = new StringBuffer();
         myBuffer.append(this.nameOfSchool+" ,"+this.yearStarted);
         return myBuffer.toString();
     }

}
___________________________________________________________________________

public class MyClassToBePersisited implements Serializable{

    static final long serialVersionUID = -3126998878902358585L;

    private Date time;
    private School school;
    private Profile profile;

    public MyClassToBePersisited(School school, Profile profile) {

        //time = Calendar.getInstance().getTime();
          this.school = school;
          this.profile = profile;
    }

    public School getSchool(){
         return school;
     }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    public Profile getProfile(){
         return profile;
     }

    public Date getTime() {
        return time;
    }


}
____________________________________________________________________________________________
public class DeserializeMyClassToBePersisted {

    public static void main(String [] args) {

        String filename = "MyClassToBePersisited.ser";
        if(args.length > 0) {
            filename = args[0];
        }

        // Deserialize the previously saved
        // PersistentTime object instance.
        MyClassToBePersisited myPers = null;
        FileInputStream fis = null;
        ObjectInputStream in = null;
        try {
            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            myPers = (MyClassToBePersisited)in.readObject();
            in.close();
        } catch(IOException ex) {
            ex.printStackTrace();
        } catch(ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        // print out restored time
        School mySchool = myPers.getSchool();
        Profile myProfile = myPers.getProfile();

        System.out.println("School: " + mySchool.toString());
        System.out.println("Profile: " + myProfile.toString());

    }
_________________________________________________________________________________

Everything seems to be working fine other than that one value.  Is there a
readResolve() or a another method you could recommend I use?  So
frustrating.


On Wed, Apr 22, 2009 at 3:14 PM, miga <[email protected]> wrote:

>
>
>
> On Apr 22, 8:14 pm, Adam Smith <[email protected]> wrote:
> > I'm having a problem with my one transient integer, yearStarted.  For
> some
> > reason, it continuously returns a '0' (instead of the desired integer).
> >
> > public MyClassToBePersisited(School school, Profile profile) {
> First, do not give parameters to your class constructor, as you cannot
> serialize them. And inside the constructor, just assign new objects to
> the private parameters. This way, you will have an object that is
> completely self-contained and therefore can be serialized and
> deserialized without loss of data.
> >         this.school = School.getSchool("", 23);
> >         this.profile = Profile.getProfile("TEST2", 77, "beach");
> >     }
> Then in the same class define accessor for reading the parameters of
> each parameter, and use them in another public method which will
> display them. And you need also accessor in the School and Profile
> classes.
> The serialization/deserialization process has to be viewed like a
> cascade of reading completely self-contained objects, and each object
> should contain methods to access the objects contained in it, etc.
>
> Hopefully it is clear for you.
> >
> > From what I understand, transient fields are not 'serialized'.
>  Therefore,
> > when I deserialize it, it automatically returns a '0'.  How the heck can
> i
> > get this to work?  I've been working on this for days now.
> >
> >         // Deserialize the previously saved
> >         // PersistentTime object instance.
> >         MyClassToBePersisited myPers = null;
> >         FileInputStream fis = null;
> >         ObjectInputStream in = null;
> >
>

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to