serialization - Transfer of a Java Serialized Object -
is possible declare instance of serializable object in 1 java program / class, repeat definitions of internal objects in different program /class entirely, , load in big complex object data file? goal able write editor items that's kept locally on build machine, write game , distribute people play game.
i'm writing game in java hobbyist project. within game, there's family of classes extend parent class, gameitem. items might in various families healingpotion, bomb, keyitem, , on.
class gameitem implements serializable { string itemname string imageresourcelocation ....} what want include definitions of how create each item in particularly family of items, have big class called gameitemlist, contains possible items can occur play game.
class gameitemlist implements serializable { linkedlist<gameitem>gameitemlist; //methods here lookupbyname, lookupbyindex return references item } maybe @ point - player starts new game, or game launches, like:
//create itemlist fileinputstream filein = new fileinputstream("items.dat"); objectinputstream in = new objectinputstream(filein); gameitemlist allitems = (gameitemlist)in.readobject(); in.close(); //now have object called allitems can used lookups. thanks guys, comments or appreciated.
when serialize object, every field of object serialized, unless marked transient. , behavior of course recursive. yes, can serialize object, deserialize it, , deserialized object have same state serialized one. different behavior make serialization useless.
i wouldn't use native serialization long-term storage of data, though. serialized objects hard inspect, impossible modify using text editor, , maintaining backward compatibility older versions of classes hard. use more open format xml or json.
Thanks, this is generally helpful.
ReplyDeleteStill, I followed step-by-step your method in this Java online training
learn Java online