Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Call a class using its name (in String object)??

Reply
Thread Tools

Call a class using its name (in String object)??

 
 
Z
Guest
Posts: n/a
 
      10-22-2007
I have a name of a class in a string object. Is there a way to call
that class using its name in a string object? I am using reflection
and I have the name of the class; is there a way to call it using that
name?

Thanks

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      10-22-2007
Z wrote:
> I have a name of a class in a string object. Is there a way to call
> that class using its name in a string object? I am using reflection
> and I have the name of the class; is there a way to call it using that
> name?


<http://java.sun.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String)>

--
Lew
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      10-22-2007
Lew wrote:
> Z wrote:
>> I have a name of a class in a string object. Is there a way to call
>> that class using its name in a string object? I am using reflection
>> and I have the name of the class; is there a way to call it using that
>> name?

>
> <http://java.sun.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String)>


By the way, there is no way to "call [a] class" in Java. You can only call
methods and constructors.

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-22-2007
On Mon, 22 Oct 2007 15:28:10 -0000, Z <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>I have a name of a class in a string object. Is there a way to call
>that class using its name in a string object? I am using reflection
>and I have the name of the class; is there a way to call it using that
>name?


see http://mindprod.com/jgloss/classforname.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      10-22-2007
On Oct 22, 8:28 am, Z <(E-Mail Removed)> wrote:
> I have a name of a class in a string object. Is there a way to call
> that class using its name in a string object? I am using reflection
> and I have the name of the class; is there a way to call it using that
> name?
>
> Thanks


You don't call a class. You can load a class and instantiate it, its
called Reflection.

Don't use reflection! Well, don't use it unless you have a *REALLY*
good reason to.
<http://virtualinfinity.net/wordpress...007/01/11/the-
dangers-of-reflection-or-put-down-that-mirror/>

Having said that, if you really want to know how, google for Java
Reflection. You can load classes, instantiate objects, call methods,
access fields, etc...

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
How to call a function by a string of name & How to get the name of aclass? cutecutemouse C++ 2 07-13-2008 09:26 PM
Determining method type given its string name presentation and its corresponding object reference. Apple Python 3 08-01-2005 03:16 AM



Advertisments