• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting dynamic  RSS feed

 
steve anson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way to cast an object to its class type dynamically
For e.g.
Object [] o = {"Hello", new Integer(2)};
How can another reference be declared based on the dynamic class type of each element in the array?
Thanks Steve
 
Angel Dobbs-Sciortino
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object.getClass().getName() returns the String name for the object.
Angel
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're looking for

you're out of luck. I mention this because several people have asked about it in the past. And I'm curious to know why.
But you can do this:

or more commonly

Hope that's what you wanted to do!
 
steve anson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks that is what i suspected!
S
 
norman richards
Author
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
If you're looking for

you're out of luck. I mention this because several people have asked about it in the past. And I'm curious to know why.


I'm curious what you think the above code would do. Casting an object doesn't fundamentally change the object in any way. What is the type of the "something" field? If you don't know the type (at compile time) that you are trying to cast to, what sense does it make to cast it for assignment to "something". You'd have to know the type or the above just doesn't make any sense.
If what you are after is the side effect of detecting the type, why not use the isInstance() method on java.lang.Class?

That's a bit more verbose, but it more clearly expresses your desire, which is to raise an exception if a dynamic type test fails. (you aren't trying to cast, but to test) If you have two Class objects, you can use isAssignableFrom() to do the same test. But that name is too confusing for my tastes.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has come up before, and I was curious about the source. Maybe something worded poorly in a school assignment? Or a book?
If you don't know what type an object is at coding time, you can't code calls to any methods on it cause you won't know what methods it has. There's nothing you can cast it to that would make thins any better. Leaves you in a pretty useless state.
Of course you can use methods and properties on nearly any object through reflection, but that's not casting. Here's a little toy I wrote that goes bonkers on reflection, and probably blows us right out of the beginner forum!
http://www.surfscranton.com/architecture/Interpreter.htm
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!