Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

If I have a String "Hello" and I know it is a name of a class, how to use it to create a object?

Thread view: 
fAnSKyer - 25 Sep 2006 21:32 GMT
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
Andrew Thompson - 25 Sep 2006 21:36 GMT
> 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?

Reflection.  But what actual general problem are
you attempting to solve?

Using reflection in Java is generally considered a
poor hack (though some types of applications
need to use it extensively - like servers).

Andrew T.
Thomas Schodt - 25 Sep 2006 21:45 GMT
> 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?

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()>
fAnSKyer - 25 Sep 2006 21:58 GMT
Thanks a lot, also to Andrew T. :P

> > 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?
[quoted text clipped - 6 lines]
> <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()>
fAnSKyer - 26 Sep 2006 03:42 GMT
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 写道:

> > 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?
[quoted text clipped - 6 lines]
> <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()>
Dijon Yu - 26 Sep 2006 06:59 GMT
> 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?

If method get() is not a static method, you must create a instance of
Hello class, you use  Object o =
Class.forName(classname).newInstance();
like Thomas Schodt write
Dijon  Yu

> Thomas Schodt 写道:
>
[quoted text clipped - 8 lines]
> > <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()>
Thomas Schodt - 26 Sep 2006 07:32 GMT
> 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?

You either need to know about the class (which defeats the purpose)
or it needs to implement a known interface

interface KnownInterface {
  Object get();
}

class UnknownClass implements KnownInterface {
  Object get() {
  // ...
  }
}

  String classname = "UnknownHello";
  Object o = Class.forName(classname).newInstance();
  KnownInterface ki = (KnownInterface)o;
  Object ret = ki.get();

or you need to get serious with reflection - read
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/package-summary.html>
fAnSKyer - 27 Sep 2006 22:47 GMT
Thanks a lot.

What if KnownInterface is also a String and I need use this String to
do the following steps?
Still need help.

Thanks, fAns.
Thomas Schodt 写道:

> > A following question,
> > It can run, but IF the class "Hello" have a method get();
[quoted text clipped - 22 lines]
> or you need to get serious with reflection - read
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/package-summary.html>
Thomas Schodt - 28 Sep 2006 10:06 GMT
> What if KnownInterface is also a String

Then it is not a known interface. o_O

> Still need help.

you need to get serious with reflection - read
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/package-summary.html>

There seems to be a number of threads around on this.
Read them.
su_dang@hotmail.com - 28 Sep 2006 16:22 GMT
> A following question,
> It can run, but IF the class "Hello" have a method get();
[quoted text clipped - 17 lines]
> > <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()>

Using reflection, you can obtain an object of the class Method which
can be used to invoke the method you want.
fAnSKyer - 02 Oct 2006 04:51 GMT
thanks and those who helped me :P
> > A following question,
> > It can run, but IF the class "Hello" have a method get();
[quoted text clipped - 20 lines]
> Using reflection, you can obtain an object of the class Method which
> can be used to invoke the method you want.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.