Skip to content

Commit aafb89c

Browse files
committed
Explore the transient reserved word usage.
1 parent 8422a69 commit aafb89c

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

Chapter11/Employee.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ public class Employee implements Serializable{
77
private int age;
88
// test compatible change with Deserialization
99
private int salary;
10+
private transient int bonus;
1011

11-
public Employee(String name, int age, int salary){
12+
public Employee(String name, int age, int salary, int bonus){
1213
this.name = name;
1314
this.age = age;
1415
this.salary = salary;
16+
this.bonus = bonus;
1517
}
1618

1719
public String getName(){
@@ -25,4 +27,8 @@ public int getAge(){
2527
public int getSalary(){
2628
return salary;
2729
}
30+
31+
public int getBonus(){
32+
return bonus;
33+
}
2834
}

Chapter11/SerializationDemo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static void main(String[] args){
2828
System.out.println("Deserialization: " + emp.getName());
2929
System.out.println("Deserialization: " + emp.getAge());
3030
System.out.println("Deserialization: " + emp.getSalary()); // output 0 as default initial value.
31+
System.out.println("Deserialization: " + emp.getBonus()); // transient field compatible: output 0.
3132
System.out.println("BONUS: " + ois.readInt());
3233
}catch(IOException ioe){
3334
ioe.printStackTrace();

0 commit comments

Comments
 (0)