instanceof is correct nothing wrong.
Think I can add n number of instance to list every thing I cannot tell it in if eles....
thats why method said me is ok
Santhosh has to decide which is better because he asked doubt
@Mohana : what is the significant/advantage of D. Ogranos's approach? Rob approach is simply. is it n't ? but generic can be even better
posted 10 years ago
Rob Prime wrote:What's wrong with instanceof?
Nothing really. I think what santhosh wanted was to provide a class (with a given name, String in the example) and have the object(s) in the list casted to that class. So my thought was to find the actual Class object with the given name, and then to cast to that class. Class has a cast() method, but it doesn't work as I expected:
So the cast() method is kind of pointless if I understand it right, and you really have to use an if-else construct and test which class you have...and then you can just use instanceof right away, as you suggested.