On Apr 23, 3:06 am, Adam Smith <[email protected]> wrote:

> public class School implements Serializable{
>
>      private String nameOfSchool;
>      transient private int yearStarted;
>
>     public String getNameOfSchool() {
>         return nameOfSchool;
>     }
>
>     public int getYearStarted() {
>         return yearStarted;
>     }
>
Get rid of the setXXX methods. You don't need them as the serialize
object is to be viewed as something frozen at the moment you
serialized it.
That means that your constructor should not use parameters.
But that does not mean you should have an empty constructor ;-) Put
something in it.
>
>      public School(){
>
>      }
>
Get rid of this static method getSchool method.

Get rid of the toString method. You don't need to access the data from
School but from MyClassToBeSerialized.
___________________________________
>
> public class MyClassToBePersisited implements Serializable{
>
>     static final long serialVersionUID = -3126998878902358585L;
>
>     private Date time;
>     private School school;
>     private Profile profile;
>
Same punition here no parameter for the constructor, that means also
no "this" methods.
>     public MyClassToBePersisited(School school, Profile profile) {
>
>         //time = Calendar.getInstance().getTime();
>           this.school = school;
>           this.profile = profile;
>     }


Get rid of the setxxx methods for same reasons as above

Change this to call the getName of whatever method of Profile class on
the profile instance you create in the constructor.
>     public Profile getProfile(){
>          return profile;
>      }
>
Add here display methods for School and Profile data.As at the moment
of the deserialization, the School and Profile objects are not known
directly, you need those methods here and access the parameters
through the instances of this class.

>
> }
> ___________________________________________________________________________ 
> _________________
> public class DeserializeMyClassToBePersisted {
>
>     public static void main(String [] args) {

I would rather wrap the whole code in the try.
>
>         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();
>         }
>
You should retrieve the name of the classes via the lookup method
>         // print out restored time
>         School mySchool = myPers.getSchool();
>         Profile myProfile = myPers.getProfile();
Here use your display methods to print the contents of the classes.
>
>         System.out.println("School: " + mySchool.toString());
>         System.out.println("Profile: " + myProfile.toString());
>
>     }

--~--~---------~--~----~------------~-------~--~----~
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