F
fAnSKyer
If I have a String "Hello" and I know it is a name of a class, how to
use the string to create a object?
Thanks, fAn
use the string to create a object?
Thanks, fAn
fAnSKyer said:If I have a String "Hello" and I know it is a name of a class, how to
use the string to create a object?
fAnSKyer said:If I have a String "Hello" and I know it is a name of a class, how to
use the string to create a object?
Thomas said:You mean like
String classname = "Hello";
Object o = Class.forName(classname).newInstance();
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#forName(java.lang.String)>
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#newInstance()>
If method get() is not a static method, you must create a instance offAnSKyer said:A following question,
It can run, but IF the class "Hello" have a method get();
using o.get() is wrong. Because it is a Object, not a Class. How to
solve then?
fAnSKyer said:A following question,
It can run, but IF the class "Hello" have a method get();
using o.get() is wrong. Because it is a Object, not a Class. How to
solve then?
fAnSKyer said:What if KnownInterface is also a String
Still need help.
fAnSKyer said:A following question,
It can run, but IF the class "Hello" have a method get();
using o.get() is wrong. Because it is a Object, not a Class. How to
solve then?
Thanks a lot
Fans.
Thomas Schodt 写é“:
Using reflection, you can obtain an object of the class Method which
can be used to invoke the method you want.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.