Or is the question regarding how to cast back to an arbitrary type?
Ziyang Zhang
Ranch Hand
Joined: Jul 17, 2010
Posts: 47
posted
0
David Newton wrote:Or is the question regarding how to cast back to an arbitrary type?
Actually, "com.project.Package1.Bark" is stored in a string. I can not get this string until runtime.
I want to cast it back to the type stored in the string.
Is this an actual use case, or an academic exercise? If you don't know the class (or an interface) in advance, you can't have a reference variable of that class and therefore no need for a cast.
Sorry, you can't do that. You can cast to any type known at compile time by the compiler, that's all.
What are you trying to achieve by this dynamic loading? There are various ways to deal with the issue but what you should do depends on your requirement.
Ziyang Zhang wrote:
Actually, "com.project.Package1.Bark" is stored in a string. I can not get this string until runtime.
I want to cast it back to the type stored in the string.
This actually doesn't make much sense. There is no reason to cast something and not use it. Meaning you cast it to something so that it can be used -- either as a parameter to a method or to be assigned to a reference variable. And in both these cases, you need the type available at compile time -- either for the method or for the variable.
Ziyang Zhang wrote:
Actually, "com.project.Package1.Bark" is stored in a string. I can not get this string until runtime.
I want to cast it back to the type stored in the string.
This actually doesn't make much sense. There is no reason to cast something and not use it. Meaning you cast it to something so that it can be used -- either as a parameter to a method or to be assigned to a reference variable. And in both these cases, you need the type available at compile time -- either for the method or for the variable.
Henry
Thank you very much for your response, Henry!
Maybe I didn't express my question well, I modified the question, please take a peek.
Thanks.
Ziyang Zhang wrote:
Thank you very much for your response, Henry!
Maybe I didn't express my question well, I modified the question, please take a peek.
Thanks.
Okay, I will take a look, but in the future, please don't do that. It kinda messes up all the answers that you received so far -- as it is now for a question that has been changed.
Ziyang Zhang wrote:Maybe I didn't express my question well, I modified the question, please take a peek.
Unfortunately the modified question isn't any better than the original question. All of the questions which people asked in response are still open and valid questions. So it's your turn to answer those questions before we can proceed.
Again, it doesn't make sense. Not only are you trying to trying to cast, you are also declaring a Bark variable. How the heck can you declare such a variable if you don't have the class in scope at compile time. Answer: You can't.
In other words, you can't even declare the Bark reference, much less try to cast the object to it.
So... how do you call the method? You need to use reflection. From the Class instance, you can get a Method instance for the method that you are trying to call. Using that Method instance, you can invoke the method, using the instance that you got back -- and without the need to cast.
And if you know the name of the method you want to call, and it's *not* a String, then you should probably use an interface, and be done with all this making-Java-something-it-isn't stuff.