How to save object variables after closing my program? How to save object variables after closing my program? database database

How to save object variables after closing my program?


I would just serialize this object and deserialize it after you resume your game. I think it is the best way. Here you can find the way how to do that. http://www.tutorialspoint.com/java/java_serialization.htm


Maybe I didn't really understant your question but if you need a method to catch the exit before the program exit (CTRL-C for instance) you van use an "exit hook", usefull also to free connection

see

Useful example of a shutdown hook in Java?


While you could of course use a database, this would be quite overkill for a simple offline game. In that case it would be more common to save the game-state to a file (a "savegame").

By using the class ObjectOutputStream, you can write objects to a file. It converts the objects into a format which can then later be read with the class ObjectInputStream .