• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

save state of variable

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only make something static if you are sure it really is "shared across the class."
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic