Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

save state of variable

 
Nishant Arora
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only make something static if you are sure it really is "shared across the class."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic