1

How can I generalize the following code:

String nameString = "myClass_t"; myClass_t myVar = (myClass_t) AnotherClass.classMethod(); 

The return type of AnotherClass.classMethod() needs to be obtained from a file (thus a String variable). The type of myVar needs to match that type.

How can I rewrite the 2nd line to enable it to get the type from nameString?

Thank you so much for your help.

2
  • 1
    You will probably want to use Reflection. But unless you post more about what you actually want to accomplish, you might as well store the result of AnotherClass.classMethod() into an Object and not worry about the type. Commented Jan 10, 2011 at 19:37
  • The thing is that I don't know myClass_t. I want to rewrite that 2nd line so it is generalized. Commented Jan 10, 2011 at 22:27

2 Answers 2

2

This doesn't make a lot of sense. If the type of myVar is not statically known, you will only be able to use it via reflection. In that case, just store it as an Object.

Are you trying to verify that classMethod() is returning the expected type? Or are you trying to cast the result to a specific type so that you can invoke specific methods on it? In other words, what are you going to do with myVar next? Invoke a method like myVar.myMethod()?


Here's an example of invoking a method on an object using reflection. Refer to the Class documentation for a more detailed explanation.

Object myVar = AnotherClass.classMethod(); /* Get the class object for "myVar" to access its members. */ Class<?> myClass = myVar.getClass(); /* Find the public, no-arg method "myMethod()". */ Method mth = myClass.getMethod("myMethod"); /* Invoke "myMethod()" on "myVar", and assign result to "r". */ Object r = mth.invoke(myVar); 
Sign up to request clarification or add additional context in comments.

5 Comments

myVar's type is not statically known. I am trying to cast the result to a specific type so I can invoke myVar.myMethod().
@Osmanthus: The syntax myVar.myMethod() requires static typing. Java is a statically typed language. If you want to invoke a method dynamically, you have to use reflection. I'll update my answer to illustrate.
Thank you. Can you clarify the line: Method mth = clz.getMethod("myMethod") please? What is clz? do you mean myClass?
@Osmanthus: yes, sorry about that. I changed the variable names after my first revision and missed that.
Thank you so much for your help. My code works after I follow your example there.
2

Use an instance of Class:

Class<?> myclass = Class.forName("myClass_t"); myClass_t myVar = (myClass_t)myclass.cast(AnotherClass.classMethod()); 

Be aware that this might throw several Exceptions, e.g. if the class defined by the string does not exist or if AnotherClass.classMethod() doesn't return an instance of the class you want to cast to. To clarify the interface idea (which is usally used for plugin mechanisms in Java):

interface Testing { public String getName(); } class Foo implements Testing { public String getName() { return "I am Foo"; } } class Bar implements Testing { public String getName() { return "I am Bar"; } } // Then Class<?> myclass = Class.forName("Foo"); Testing instance = (Testing)myclass.newInstance(); System.out.println(instance.getName()); // I am a Foo myclass = Class.forName("Bar"); Testing instance = (Testing)myclass.newInstance(); System.out.println(instance.getName()); // I am a Bar 

Basically you have a dynamic class name (e.g. from a properties file). This class implements an interface, so you can make sure that class instances provide the interfaces methods (instead of using reflection for everything).

9 Comments

You can't assign Class<?> to Class<myClass_t>. And even if you could, why not just use myClass_t.class? Dynamically loading the class is not necessary; it's a static type in your example.
That won't work, since in the second line there is no type myClass_t. The type of myvar will have to be Object or some known superclass/interface of myClass_t.
The idea of using reflection is of course correct. Anyway, I'm wondering, why this example is voted up, as it won't work...
-1. Doesn't compile, and even if it did it would be pointless.
I already corrected it and it should work. It also answers the question (even if using a static string instead of the class name itself doesn't make sense in that context).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.