Creating an object based on class name

Discussion in 'Java' started by Wes Harrison, Aug 27, 2004.

  1. Wes Harrison

    Wes Harrison Guest

    Is it possible to create a new instance of a class whose name is in a String
    variable? If so, could you please show me how it's done?

    Thanks,

    Wes
     
    Wes Harrison, Aug 27, 2004
    #1
    1. Advertising

  2. Wes Harrison <> scribbled the following:
    > Is it possible to create a new instance of a class whose name is in a String
    > variable? If so, could you please show me how it's done?


    Look up java.lang.Class.forName() and java.lang.Class.newInstance().
    Also remember that if you are trying to cast it into a type whose name
    is in a String variable, or cast it into something a Class object
    represents, you're on the wrong track. As quite explicitly stated
    elsewhere on this newsgroup, variable type casting in Java happens
    completely statically.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "It sure is cool having money and chicks."
    - Beavis and Butt-head
     
    Joona I Palaste, Aug 27, 2004
    #2
    1. Advertising

  3. Wes Harrison

    F Guest

    Wes Harrison <> wrote:
    > Is it possible to create a new instance of a class whose name is in a String
    > variable? If so, could you please show me how it's done?
    >
    > Thanks,
    >
    > Wes
    >

    String className="java.util.Sting";
    String s=(String)(Class.forName(className)).newInstance();


    f.
     
    F, Aug 27, 2004
    #3
  4. F <> scribbled the following:
    > Wes Harrison <> wrote:
    >> Is it possible to create a new instance of a class whose name is in a String
    >> variable? If so, could you please show me how it's done?
    >>

    > String className="java.util.Sting";
    > String s=(String)(Class.forName(className)).newInstance();


    java.lang.String is a final class and thus it is impossible for a
    class called java.util.Sting to extend it. Therefore the second line
    will throw a ClassCastException.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Life without ostriches is like coffee with milk."
    - Mika P. Nieminen
     
    Joona I Palaste, Aug 27, 2004
    #4
  5. Wes Harrison

    Bryce Guest

    On Fri, 27 Aug 2004 19:52:02 +1000, "Wes Harrison" <>
    wrote:

    >Is it possible to create a new instance of a class whose name is in a String
    >variable? If so, could you please show me how it's done?


    If you want to instantiate a class, and know it has a default
    constructor (i.e., no parameters) then:

    Class.forName(classNameAsString).newInstance();

    Generally, you do this if you know it implements an interface or
    abstract class. If you know nothing about the class, then you wouldn't
    know what methods to call (although, you could use reflection, but
    that would be somewhat random, as you wouldn't know beforehand what to
    expect).

    If you want to instantiate a class, and know what parameters are
    passed in the constructor, you can do that as well.

    Check out here:
    http://javaalmanac.com/egs/java.lang.reflect/Constructors.html

    --
    now with more cowbell
     
    Bryce, Aug 27, 2004
    #5
  6. Joona I Palaste wrote:

    > F <> scribbled the following:
    >> Wes Harrison <> wrote:
    >>> Is it possible to create a new instance of a class whose name is in a
    >>> String
    >>> variable? If so, could you please show me how it's done?
    >>>

    >> String className="java.util.Sting";
    >> String s=(String)(Class.forName(className)).newInstance();

    >
    > java.lang.String is a final class and thus it is impossible for a
    > class called java.util.Sting to extend it. Therefore the second line
    > will throw a ClassCastException.


    I'm pretty sure F meant to say String className="java.lang.String"; instead,
    in which case the construct works perfectly.
    Nothing to do with extending final classes.

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Aug 27, 2004
    #6
    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. ding feng
    Replies:
    2
    Views:
    2,904
    ding feng
    Jun 25, 2003
  2. Replies:
    10
    Views:
    550
    Jim Langston
    May 25, 2006
  3. markla
    Replies:
    1
    Views:
    580
    Steven Cheng
    Oct 6, 2008
  4. gentlestone
    Replies:
    6
    Views:
    995
    Bruno Desthuilliers
    Oct 6, 2009
  5. Collin Moore
    Replies:
    2
    Views:
    128
    Bertram Scharpf
    Oct 15, 2009
Loading...

Share This Page