Hi. I have a little problem while implementing the homework 1043. Everything works fine, but the transient field
int yearStarted (this is transient field)
everytime is 0;
Here is my Class School:
public class School implements Serializable{
private String nameOfSchool;
transient private int age;
public School(String nameOfSchool, int age){
this.nameOfSchool = nameOfSchool;
this.age = age;
}
public static School getSchool(String nameOfSchool, int age){
return new School(nameOfSchool, age);
}
@Override
public String toString(){
StringBuffer myBuffer = new StringBuffer();
myBuffer.append(this.nameOfSchool+" ,"+this.age);
return myBuffer.toString();
}
}
here is the serializeMain:
public class SerializeMyClass{
public static void main(String [] args){
String filename = "MyClassToBePersisted.ser";
if(args.length > 0) {
filename = args[0];
}
MyClassToBePersisted myPersisted = new
MyClassToBePersisted(School.getSchool("Scholname2", 6),
Profile.generateProfile("ProfileName2", 99, "myHobby2"));
// Serialize the object instance and save it in
// a file.
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
out.writeObject(myPersisted);
out.close();
} catch(IOException ex) {
ex.printStackTrace();
}
System.out.println("Current time is saved into " + filename);
}
}
here is the DeserializeMain:
public class DeserializeMyClass {
public static void main(String [] args) {
String filename = "MyClassToBePersisted.ser";
if(args.length > 0) {
filename = args[0];
}
// Deserialize the previously saved
// PersistentTime object instance.
MyClassToBePersisted myPers = null;
FileInputStream fis = null;
ObjectInputStream in = null;
try {
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
myPers = (MyClassToBePersisted)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());
}
}
Thanks for your help.
smime.p7s
Description: S/MIME cryptographic signature
