i load a class using
Class.forName(klassname,false,loader)
After this i create an instance using
klass.newInstance();
It returns an object type.I want to cast it to specific type(ie.Klassnamw instance).I used normal casting but it gets hung because it is not resolved during runtime.How can i cast it?Hellp
Casting is usually used to give the compiler more information. You don’t have that information at compile-time, so you can’t give it to the compiler.
Moreover, the point of casting is usually so that you can get to some member of the class which wouldn’t be known otherwise – but if you don’t know the class until execution time, how can you know the members?
There are certain cases where it would be nice, but they’re pretty uncommon. What are you trying to do with the instance after you’ve created it? If you’re trying to call methods which you do know about at compile time, can you make those methods part of an interface and cast to the interface?