This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes value of a static variable on being serialized Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "value of a static variable on being serialized" Watch "value of a static variable on being serialized" New topic
Author

value of a static variable on being serialized

Alicia Logan
Greenhorn

Joined: Aug 24, 2005
Posts: 14
Suppose i have a class with a static variable i = 10. Now i serialize this class and write the object of this class to a file. What will be value of i?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Try it and see.


Joanne
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

The value of the static variable won't be serialized. When the object is deserialized into a new JVM, assuming the class hadn't already been loaded, the class will first be loaded normally. That means that any static initializers will be executed, so that the value of that static variables will be whatever it is right after the class is loaded. If the class had already been loaded, then the static variable won't be affected by the object being deserialized.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: value of a static variable on being serialized
 
Similar Threads
Getting value of static variable with Reflection ?
fundamentals
Static variables
Getting problem in declaring static variable.
Master exam question