Java: Reflection against casting when you know superclass

Posted by Ema on Stack Overflow See other posts from Stack Overflow or by Ema
Published on 2012-07-11T08:57:48Z Indexed on 2012/07/11 9:15 UTC
Read the original article Hit count: 207

Filed under:
|
|

I don't know exactly how to define my doubt so please be patient if the question has already been asked.

Let's say I have to dinamically instantiate an object. This object will surely be instance of a subclass of a known, immutable class A. I can obtain dinamically the specific implementation class.

Would it be better to use reflection exactly as if I didn't know anything about the target class, or would it be preferrable/possible to do something like:

A obj = (Class.forName("com.package.Sub-A")) new A();

where Sub-A extends A ? The purpose would be to avoid reflection overhead times...

Thank you in advance.

© Stack Overflow or respective owner

Related posts about java

Related posts about reflection