File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization is not for static variables - Really true? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization is not for static variables - Really true?" Watch "Serialization is not for static variables - Really true?" New topic
Author

Serialization is not for static variables - Really true?

Steffen Hauser
Greenhorn

Joined: Dec 04, 2007
Posts: 6
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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


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



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
binayakumar patel
Greenhorn

Joined: Jun 26, 2009
Posts: 27
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

Joined: Apr 20, 2009
Posts: 804
    
    1

OK, that's right.
So the static variables get their default values or the values they were initialized by?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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
 
Consider Paul's rocket mass heater.
 
subject: Serialization is not for static variables - Really true?
 
Similar Threads
Related to Object Serialization
Serialization
serialization question
Can't Understand How It Comes In Deserialization
how FileOutputStream and ObjectOutputStream working here