• Post Reply Bookmark Topic Watch Topic
  • New Topic

runtime class casts  RSS feed

 
sander hautvast
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there ANY way to cast some Object to a class that isn't known until runtime? ??
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehm, this is one of the fundamentals of Java. For example:
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I don't think that's what he's asking. The type String is known at compile time -- he wants to do something like



And the answer is no, there's no way to do anything like this. But if you think about it, what could you possibly do with such an object once you cast it? Any method calls would have had to be compiled, but since the type wasn't known, that couldn't have happened.

There are two different things you can do. One is to cast to an interfacce or supertype. If the unknown object is known to implement some interface, you can compile in a cast to that interface, and indeed, this is a very common thing to do.

If you know absolutely nothing about the object at compile time, then you don't know the names or signatures of any methods you'd want to call, either, so presumably these are also discovered at runtime. You can still call such methods, and even access unknown member variables, using the classes in the java.lang.reflect package. These let you, for example, ask for all the methods of an object, find out the arguments each one takes, and then invoke them, all by working with the java.lang.reflect.method class.
 
sander hautvast
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok silly question,
based on the misconception of the workings of cast. thought there was some state of a class indicating the runtime class... got me??

so Object o=Class.forName(x).newInstance()
would be of type Object, and i thought that
o.getMethods() would yield only methods belonging to Object where o would be instanceof AnyOtherClass
Wrong there.)
 
Dan Johnsson
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sander hautvast:
ok silly question,
based on the misconception of the workings of cast. thought there was some state of a class indicating the runtime class... got me??

so Object o=Class.forName(x).newInstance()
would be of type Object, and i thought that
o.getMethods() would yield only methods belonging to Object where o would be instanceof AnyOtherClass
Wrong there.)


I guess you did dig into the reflection lib? Lots of cool stuff around. Easy to shoot away foot, leg and torso ...

I've actually seen people shoot away the hand holding the gun using the reflection library.

/Dan
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sander hautvast:
ok silly question,
based on the misconception of the workings of cast. thought there was some state of a class indicating the runtime class... got me??


An object has a "runtime type" -- it's the "real" type of an object. It generally works just the opposite of what you're saying here. If you have a variable "o" of type Object pointing to a SomeOtherClass object, and you ask

if (o instanceof SomeOtherClass)

you get "true". If you ask for the methods of the object pointed to by "o", you get the methods of SomeOtherClass, not the methods of Object.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!