how should I use .getClass() ?

adding objects to a Vector, I'd like to retreive them with the correct original class type :

Vector v = new Vector();

User visitor = new User(1,"joe");

History hist = new History(1);

v.addElement(visitor);

v.addElement(hist);

Object oVisitor = v.elementAt(0);

Object oHist = v.elementAt(1);

// of course here I know the class, but I want to guess before cast :

(oVisitor.getClass().getName())oVisitor.aMethodInUser();

// doesn't work.

may be I should eval() the last code line but I don't know if it's possible in Java ?

anyone a better idea?

thanks for your help.

Laurent.

[668 byte] By [cactusssa] at [2007-9-28 5:26:33]
# 1
You could use instanceof that tells you the Dynamic class of your object. And ((User)oVisitor).aMethodInUser() to call a method on userBut the best way is maybe to have a superclass, usually you don't want to have heterogeneous Vectors.
jyjeanyvesa at 2007-7-9 16:37:25 > top of Java-index,Other Topics,Java Community Process (JCP) Program...