[java] Typecasting Woes

Started by
1 comment, last by Ghardoan 22 years, 4 months ago
Hey guys. Recently, I came up with the following dilemma: My game deals with several subclasses of a Monster class, such as Spider and Goblin. Everything is fine until I need to typecast an Object read from an ObjectInputStream to its original subclass, rather than typecasting it to the general Monster class. My question: Is there a better way to get the monster''s subclass than actually reading a serialized Class object before the monster? In other words, is there a better way than this: Class monsterClass = (Class)in.readObject(); monsterClass myMonster = (monsterClass)in.readObject(); I don''t really want to serialize a Class object for each monster (or even groups of monsters) in my files if I don''t have to. Is there an alternative? Thanks in advance, Ghardoan
Advertisement
You can call the getClass() method on any object to retrieve its Class object, no need to serialize it separately.
Thanks HenryApe. I actually just figured this out myself after writing a quick object serialization test program.

Ghardoan

This topic is closed to new replies.

Advertisement