Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes save state of variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "save state of variable " Watch "save state of variable " New topic
Author

save state of variable

Nishant Arora
Greenhorn

Joined: Apr 21, 2009
Posts: 16
I want to increement a variable each time jvm loads the file or in other words i want to save the state of variable . Can anybody suggest how can i do so?
can anybody post the simple code for this. I googled for it but didn't got a small program demonstrating it.
Prabz Bhatia
Greenhorn

Joined: Apr 14, 2009
Posts: 19
well you can serialize an entire object, i e store values of all instance variables in a file , and can load it at will. you can find a simple program in Head First Java . Google also helps...
Nishant Arora
Greenhorn

Joined: Apr 21, 2009
Posts: 16
I have tried to code but its not saving state of variable classLoadCount where is the problem??

This for writing object ......................................................


and this is for reading object..........................................


Prabz Bhatia
Greenhorn

Joined: Apr 14, 2009
Posts: 19
Nishant Arora wrote:I have tried to code but its not saving state of variable classLoadCount where is the problem??




Static variables aren't serialized... Static means "one per class" not "one per object".


So just remove the static keyword, and you can save the state of your object...
Prabz Bhatia
Greenhorn

Joined: Apr 14, 2009
Posts: 19
However, if you wish to save states of variables between every class instantiation,(and share them) you could simply make the variables static, and directly access the variables.... no need of serialization for this limited purpose...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37991
    
  22
Only make something static if you are sure it really is "shared across the class."
 
wood burning stoves
 
subject: save state of variable
 
Similar Threads
transient variable ? is there any other way
Saving esacpe characters to file
What does transient and native mean?
Problems with Broadcast receiver
STATE_SAVING_METHOD