File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization is not for static variables - Really true?

 
Steffen Hauser
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

extract from the K&B book(chapter 6, page 456):

Static variables are NEVER saved as part of the object's state…because they do not belong to the object!

So why is the output of the following code "55" and not "21"?



Regards,
-Steffen-
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why is the output of the following code "55" and not "21"?


Maybe this slight modification to your code can explain it...



Henry
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables are class-level variables; there is only one copy of the variable which is shared by all instances of that class.

In line 6, you set i to 55, but remember that there is only one copy of i shared by all instances of class SerTest. So i will be 55 also for the new SerTest object that you read in from the serialized file in line 14 - that's to be expected, and does not demonstrate that the static variable is serialized.
 
binayakumar patel
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Variables
When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. In the case of the Bicycle class, the instance variables are cadence, gear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations.
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables.

Thats why the value get change....
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that's right.
So the static variables get their default values or the values they were initialized by?
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lukas Smith wrote:OK, that's right.
So the static variables get their default values or the values they were initialized by?


Static variables are initialized when the classes are loaded. They are not affected, when an instance of the class is created.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic