Skip to main content
gamedev.net
🔒 Locked

[java] Typecasting Woes

Started by Ghardoan Jan 5, 2002 at 12:25 PM 1 replies 500+ views
Original Post
Ghardoan
Ghardoan
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
HenryApe
HenryApe
You can call the getClass() method on any object to retrieve its Class object, no need to serialize it separately.
Ghardoan
Ghardoan
Thanks HenryApe. I actually just figured this out myself after writing a quick object serialization test program.

Ghardoan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.