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

Discussion in 'Java' started by Z, Oct 22, 2007.

  1. Z

    Z Guest

    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
     
    Z, Oct 22, 2007
    #1
    1. Advertising

  2. Z

    Lew Guest

    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
     
    Lew, Oct 22, 2007
    #2
    1. Advertising

  3. Z

    Lew Guest

    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
     
    Lew, Oct 22, 2007
    #3
  4. Z

    Roedy Green Guest

    On Mon, 22 Oct 2007 15:28:10 -0000, Z <> 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
     
    Roedy Green, Oct 22, 2007
    #4
  5. Z

    Daniel Pitts Guest

    On Oct 22, 8:28 am, 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?
    >
    > 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/program-design/2007/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/>
     
    Daniel Pitts, Oct 22, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wiz
    Replies:
    3
    Views:
    451
    Christophe Vanfleteren
    Dec 22, 2003
  2. Apple
    Replies:
    3
    Views:
    311
    Apple
    Aug 1, 2005
  3. thunk
    Replies:
    1
    Views:
    330
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    508
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    643
    thunk
    Apr 3, 2010
Loading...

Share This Page