ObjectInputStream returns a wrong boolean value of a object

ObjectOutputStream.writeObject once, then change the boolean value of the Contact object, ObjectOutputStream.writeObject again, but when ObjectInputStream.readObject, it returns SAME boolean values which should be different. can somebody explain me this? please. thx

1.import java.io.*;

2.import java.util.*;

3.

4.publicclass TestSerializable{

5.

6./** Creates a new instance of TestSerializable */

7.publicstaticvoid main(String args[]){

8.new TestSerializable();

9.}

10.

11.public TestSerializable(){

12.try{

13. Contact contact =new Contact("email","group",1);// contact.isOnline = true for default;

14.

15. FileOutputStream fos =new FileOutputStream("ser");

16. ObjectOutputStream oos =new ObjectOutputStream(fos);

17.

18. oos.writeObject(contact);

19.

20. contact.setOnline(false);

21. oos.writeObject(contact);

22.

23. oos.flush();

24.

25.

26. FileInputStream fis =new FileInputStream("ser");

27. ObjectInputStream ois =new ObjectInputStream(fis);

28. Contact ccRead1 = (Contact)ois.readObject();

29. Contact ccRead2 = (Contact)ois.readObject();

30.

31. System.out.println("ccRead1.isOnline() = "+ccRead1.isOnline());

[u] 32. System.out.println("ccRead2.isOnline() = "+ccRead2.isOnline() +" which should be FALSE. ERROR"); [/u]

33.

34. oos.close();

35. ois.close();

36.

37.}

38.catch(Exception e){

39. e.printStackTrace();

40.}

41.

42.}

43.

44.}

45.

46.

47.

48.

49.

50.import javax.swing.tree.DefaultMutableTreeNode;

51.import javax.swing.tree.*;

52.import java.util.*;

53.

54.publicclass Contactextends DefaultMutableTreeNode{

55.

56.private String email;

57.privateint arrayIndex;

58.private String group;

59.privateboolean online;

60.

61.

62.public Contact(String email,String group,int arrayIndex,boolean online){

63. this.email = email;

64. this.group = group;

65. this.setArrayIndex(arrayIndex);

66. this.setOnline(online);

67. this.setUserObject(this);

68. this.setAllowsChildren(false);

69.}

70.

71.public Contact(String email, String group){

72.this(email,group,-1,false);

73.}

74.

75.public Contact(String email, String group,int arrayIndex){

76.this(email,group,arrayIndex,true);

77.}

78.

79.

80.

81.public String toString(){

82.return this.email;

83.}

84.

85.public String getEmail(){

86.return email;

87.}

88.

89.publicint getArrayIndex(){

90.return arrayIndex;

91.}

92.

93.public String getGroup(){

94.return group;

95.}

96.

97.publicboolean isOnline(){

98.return online;

99.}

100.

101.publicvoid setOnline(boolean online){

102. this.online = online;

103.}

104.

105.publicvoid setArrayIndex(int arrayIndex){

106. this.arrayIndex = arrayIndex;

107.}

108.

109.

110.}

[6499 byte] By [junmina] at [2007-11-27 2:44:07]
# 1
ObjectOutputStream caches objects and if the same object is written more than once it just sends information to tell the receiving ObjectInputStream which object to use. You can solve this problem by doing a reset() on the ObjectOutputStream before sending. This clears the cache.
sabre150a at 2007-7-12 3:10:27 > top of Java-index,Core,Core APIs...
# 2
got it, thx!
junmina at 2007-7-12 3:10:27 > top of Java-index,Core,Core APIs...
# 3
can other things cause ObjectInputStream return a wrong value?i had put the reset() method and get the problem above resolved ...but i am encounting the same problem again, and the serializable object is Contact which is the same with the one in above code.please!~~~
junmina at 2007-7-12 3:10:27 > top of Java-index,Core,Core APIs...
# 4

Did you try the reset() technique again?

You could also have a look at ObjectOutputStream.writeUnshared().

And it's not returning a 'wrong value', it is conserving the object graph of the original object sent, which is what it's specified to do and what it's supposed to do. If you don't want that behaviour use reset() or writeUnshared().

ejpa at 2007-7-12 3:10:27 > top of Java-index,Core,Core APIs...