forked from slgobinath/Java-Helps-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
83 lines (73 loc) · 1.73 KB
/
Student.java
File metadata and controls
83 lines (73 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Calendar;
/**
* Student class
*
* @author gobinath
*
*/
public class Student implements Serializable {
// Serial version ID
private static final long serialVersionUID = 5230549922091722630L;
private String name;
private transient int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
/**
* This method is called during serialization.
*
* @param oos
*/
private void writeObject(ObjectOutputStream oos) {
// Create a calendar object of current date
Calendar current = Calendar.getInstance();
// Get the current year
int currentYear = current.get(Calendar.YEAR);
// Calculate the birth year
int birthYear = currentYear - age;
try {
// Write the default attributes first
oos.defaultWriteObject();
// Write the birth year
oos.writeInt(birthYear);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method is called during deserialization.
*
* @param ois
*/
private void readObject(ObjectInputStream ois) {
// Create a calendar object of current date
Calendar current = Calendar.getInstance();
// Get the current year
int currentYear = current.get(Calendar.YEAR);
try {
// Read the default attributes first
ois.defaultReadObject();
// Read the birth year
int birthYear = ois.readInt();
// Calculate the age
this.age = currentYear - birthYear;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}