0

basic casting should be

MyClass mc = (MyClass)aClass

that is easy

but based on my program, I don't know the class name until runtime.

for example, the class name could be interp_0, interp_1, interp_2, interp_3 .......#;

Is there anyway in java that I could use to cast it?

For now All I got is

Class afterCast = Class.forName("Interp_" + countState);

but what I want is

("Interp_" + countState) afterCast

, not

Class afterCast

.

Thanks for all of you who help me. It is so quick than I expected.

RobinBattle
  • 220
  • 4
  • 14
  • 3
    And what type is the variable that you cast into? You can't declare that type (as you don't know it), so i don't see the sense in what you want to do. Probably the best way to go is to define a common subclass type for all your Interp_* types and cast into that. – okrumnow Feb 03 '12 at 08:32

3 Answers3

0
Object something = "something";
String theType = "java.lang.String";
Class<?> theClass = Class.forName(theType);
Object obj = theClass.cast(something);

This seems like a similar question: java: how can i do dynamic casting of a variable from one type to another?

Community
  • 1
  • 1
danielbeard
  • 9,120
  • 3
  • 44
  • 58
0

What you're looking for is probably something like this:

Class newClass = Class.forName("Interp_" + countState);
newClass.cast(yourObject);

However, without knowing the actual class at compile time, there wouldn't seem to be anything meaningful you can actually to with the properly casted value anyway, since you wouldn't be able to express any specific method calls or field references in your program without a specific class. Are you sure you aren't confused about something else? What is it that you are really trying to accomplish?

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • Your solution does not work, the result type of the second statement is still Object and I don't see a way how it could be made work this way. – Michael Schmeißer Feb 03 '12 at 08:38
  • Exactly. That's what I wrote in the following paragraph. It does properly give a `ClassCastException` if it doesn't match the given type, though. :) – Dolda2000 Feb 03 '12 at 08:44
  • That's true, but even if it would work you are still not able to use the methods of this object in your code, because the return value of the cast statement itself would need to be casted to this dynamic type which is the original problem again. – Michael Schmeißer Feb 03 '12 at 08:50
  • like Michael Schmeißer said, I can't use method – RobinBattle Feb 03 '12 at 09:00
  • @Dolda2000 I see, but I still don't get where your answer is an answer to the question then, it rather asks for clarification doesn't it? – Michael Schmeißer Feb 03 '12 at 09:01
  • 1
    It does answer the direct question, which was how to use a class found at runtime to reflectively cast a given instance. If the question was a mere API question (which it could have been, given some unexpected context), the sought answer would have been the Class.cast method, which I tried to illustrate. I then go on, however, to point out the obvious problems of the context and thus point out that some clarification might be in order. Happy? :) – Dolda2000 Feb 03 '12 at 09:05
  • my bad, I try to cast a class to a type which I don't know when I write down the code, but I know the pattern of the class name. I can't just say cast something to (interp_ + i), because compiler would give me an error. I don't know what to do next – RobinBattle Feb 03 '12 at 09:07
  • @user1187029: What is it, then, that you actually expect to do with the casted value? Please update your question with that information. – Dolda2000 Feb 03 '12 at 09:09
0

I would suggest using an interface implemented by all these classes which contains the methods you need. You can then cast to this interface. I don't see why this should be done different because you know which methods you expect at those objects which is an interface actually.

Michael Schmeißer
  • 3,407
  • 1
  • 19
  • 32
  • that is a good thoughts, I haven't think in this way. But it is not work in my cast. interface method cannot be static. My method had to – RobinBattle Feb 03 '12 at 08:59
  • @user1187029 That's true, but you should consider the methods not to be static then, because it is really hard to deal with common static methods of different types in a uniform way. However, if you need a solution to this as it is, you can use reflection. `Class> myClass.getMethod("methodName", argTypes)`, see http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html – Michael Schmeißer Feb 03 '12 at 09:07
  • Thanks for the help. Maybe the way I deal with this is wrong. I try to build a silly java interpreter. I ll try to use javacompiler to compile the file. – RobinBattle Feb 03 '12 at 09:34