• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get an Object using reflection, how to cast it back to a specific type?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks a lot!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how you cast objects:
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or is the question regarding how to cast back to an arbitrary type?
 
Ziyang Zhang
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

What's the actual need for this?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ziyang Zhang
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

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.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Henry
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, please, "ninja edits" are not appreciated.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.

Henry

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic