Instantiating a Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vijay Singh

    Instantiating a Class

    How do you instantiat a class with a Class.forName( ) method, if the class's
    default constructor requires a parameter?


  • D Goldman

    #2
    Re: Instantiating a Class

    "Vijay Singh" <vij_singh@hotm ail.com> wrote in message news:<efq4b.198 2$3B2.908@news-binary.blueyond er.co.uk>...[color=blue]
    > How do you instantiat a class with a Class.forName( ) method, if the class's
    > default constructor requires a parameter?[/color]

    The Class.forName(. ..) method only loads the class and executes any
    static initializers there may be. It does not instantiate any objects
    of the class (except if a static initializer does that). If you write
    Class.forName(" X").newInstance (), however, that will create a new
    instance of the class, using the constructor with zero parameters.

    You can do the following

    You can only use the default constructor however (the one with
    no parameters), if you wish to pass in parameters you must
    create an init() method (or similar) for the class being created
    and then you can call it as:

    Class class = Class.forName(" MyClass");
    Object object = class.newInstan ce();
    MyClass myClass = (MyClass) object;
    myClass.init(in t myParameter, MyOtherClasses myParameter2);

    Hope this helps,
    Daniel Goldman

    Comment

    • Soren

      #3
      Re: Instantiating a Class

      D Goldman wrote:[color=blue]
      > "Vijay Singh" <vij_singh@hotm ail.com> wrote in message news:<efq4b.198 2$3B2.908@news-binary.blueyond er.co.uk>...
      >[color=green]
      >>How do you instantiat a class with a Class.forName( ) method, if the class's
      >>default constructor requires a parameter?[/color]
      >
      >
      > The Class.forName(. ..) method only loads the class and executes any
      > static initializers there may be. It does not instantiate any objects
      > of the class (except if a static initializer does that). If you write
      > Class.forName(" X").newInstance (), however, that will create a new
      > instance of the class, using the constructor with zero parameters.
      >
      > You can do the following
      >
      > You can only use the default constructor however (the one with
      > no parameters), if you wish to pass in parameters you must
      > create an init() method (or similar) for the class being created
      > and then you can call it as:
      >
      > Class class = Class.forName(" MyClass");
      > Object object = class.newInstan ce();
      > MyClass myClass = (MyClass) object;
      > myClass.init(in t myParameter, MyOtherClasses myParameter2);
      >
      > Hope this helps,
      > Daniel Goldman
      > http://d-goldman.org[/color]

      If your class Foo has a constructor like, say

      Foo (int i, String s) {...}


      You might use
      Class cfoo = Class.forName(" Foo");
      Constructor con = cfoo.getConstru ctor(Integer.TY PE, String.class);
      Object o = con.newInstance (new Object[]{new Integer(27), "Hi !"});
      Foo foo = (Foo)o;

      add appropriate exception handling :)

      Soren

      Comment

      Working...