Simple question but I have spent over an hour with this. My code is below. I need to make SomeClass sc dynamic. So you pass the class name as a string in a function and use that class in place of static someClass. How to go about it?
SomeClass sc;
if (someOtherClassObject instanceof SomeClass){
sc=(SomeClass) someOtherClassObject;
What I want is
public void castDynamic (String strClassName){
//cast the classname referred by strClassName to SomeClass
//if it is the instance of SomeClass
}
EDIT: The above was simplification. The actual code is this
public void X(String className, RequestInterface request)
{
//My current code is this, I need to change so that "XRequest"
//can be any class referred by "className",
//and "request.getRequest" the object belonging to "className" class
//I don't want static XRequest xvr, it should be fetched dynamically
XRequest xvr;
if (request.getRequest() instanceof XRequest){
xvr=(XRequest) request.getRequest();
client.setRequest(xvr);
}
}
Another simple rephrase: I get an object using request.getRequest(). I have no clue what that object is. So I need to cast it to the classstring name provided. How to do that? That's all. – SQC 13 mins ago
You can do dynamic casting by calling a method which takes a parameter of the type you want to change your current type to. Take this for example: public class Example{ Example e1=new Example(); private Object obj=new Object(); e1.
To answer your questions. Up casting usually costs virtually nothing, (when you change the reference type to a parent class of the object). Knowledge of the reference type is enough to decide if uptyping is valid, it just gets the class loader to look up the inheritance map.
It only changes when you assign a new object. (It never changes for a given object, no object instance can change its class). The cast bridges the two: It checks the runtime type and then allows you to declare that type.
If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion, and if not then they need to be cast or converted explicitly.
You want to instantiate a class by it's name?
First of all, you need to make a Class<?>
object:
Class<?> cls = Class.forName(strClassName);
Then instantiate this (note, this can throw various exceptions - access violation, ClassNotFound
, no public constructor without arguments etc.)
Object instance = cls.newInstance();
Then you can cast it:
return (SomeClass) instance;
Please make sure you understand the differences between:
You can also cast the cls
object to have type Class<? extends SomeClass>
, if you want. It doesn't give you much, though. And you can inline it, to:
return (SomeClass)(Class.forName(strClassName).newInstance());
Oh, but you can do type checking with the cls
object, before instantiating it. So you only instanciate it, if it satisfies your API (implements the interface you want to get).
EDIT: add further example code, towards reflection.
For example:
if (cls.isInstance(request)) {
// ...
}
For invoking methods, you either need to know an interface that you can cast to, or use reflection (the getMethod
methods of the cls
object):
Method getRequest = cls.getMethod("getRequest");
getRequest.invoke(request);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With