Sending parameters using newInstance()?

Discussion in 'Java' started by frank, Jul 26, 2005.

  1. frank

    frank Guest

    Was wondering when using something like this
    theClass= (myClass)Class.forName(className).newInstance();

    it calls the classes (className in this case) default constructor with
    no arguments right? How do you send values/arguments to it? Or can't
    that be done?

    Thanks,

    Frank
     
    frank, Jul 26, 2005
    #1
    1. Advertising

  2. frank wrote:
    > Was wondering when using something like this
    > theClass= (myClass)Class.forName(className).newInstance();
    >
    > it calls the classes (className in this case) default constructor with
    > no arguments right? How do you send values/arguments to it? Or can't
    > that be done?
    >
    > Thanks,
    >
    > Frank
    >


    The Class newInstance method is a simple way of doing simple cases.

    Use the getConstructor method in the Class object to obtain the
    Constructor for your parameter types. Constructor has a newInstance
    method that takes a parameter array.

    Patricia
     
    Patricia Shanahan, Jul 26, 2005
    #2
    1. Advertising

  3. frank

    frank Guest

    Thanks would you happen to have an example?

    Patricia Shanahan wrote:

    > frank wrote:
    >
    >> Was wondering when using something like this
    >> theClass= (myClass)Class.forName(className).newInstance();
    >>
    >> it calls the classes (className in this case) default constructor with
    >> no arguments right? How do you send values/arguments to it? Or can't
    >> that be done?
    >>
    >> Thanks,
    >>
    >> Frank
    >>

    >
    > The Class newInstance method is a simple way of doing simple cases.
    >
    > Use the getConstructor method in the Class object to obtain the
    > Constructor for your parameter types. Constructor has a newInstance
    > method that takes a parameter array.
    >
    > Patricia
     
    frank, Jul 26, 2005
    #3
  4. frank

    Adam Maass Guest

    "frank" <> wrote:
    >
    > Patricia Shanahan wrote:
    >
    >> frank wrote:
    >>
    >>> Was wondering when using something like this
    >>> theClass= (myClass)Class.forName(className).newInstance();
    >>>
    >>> it calls the classes (className in this case) default constructor with
    >>> no arguments right? How do you send values/arguments to it? Or can't
    >>> that be done?
    >>>
    >>> Thanks,
    >>>
    >>> Frank
    >>>

    >>
    >> The Class newInstance method is a simple way of doing simple cases.
    >>
    >> Use the getConstructor method in the Class object to obtain the
    >> Constructor for your parameter types. Constructor has a newInstance
    >> method that takes a parameter array.
    >>
    >> Patricia

    >
    > Thanks would you happen to have an example?


    Assume you have a class like this:

    class A {
    A (int i, float f) { ... }
    }


    then you can write code like this:

    Constructor c = A.class.getConstructor(new Class[]{Integer.TYPE,
    Float.TYPE});
    A a = (A)c.newInstance(new Object[]{new Integer(1), new Float(1.0f)});
     
    Adam Maass, Jul 26, 2005
    #4
  5. frank

    A_Wieminer Guest

    >>>>Was wondering when using something like this
    >>>>theClass= (myClass)Class.forName(className).newInstance();
    >>>>
    >>>>it calls the classes (className in this case) default constructor with
    >>>>no arguments right? How do you send values/arguments to it? Or can't
    >>>>that be done?


    >>>The Class newInstance method is a simple way of doing simple cases.
    >>>Use the getConstructor method in the Class object to obtain the
    >>>Constructor for your parameter types. Constructor has a newInstance
    >>>method that takes a parameter array.
    >>>


    > Assume you have a class like this:
    > class A {
    > A (int i, float f) { ... }
    > }
    >
    > Constructor c = A.class.getConstructor(new Class[]{Integer.TYPE,
    > Float.TYPE});
    > A a = (A)c.newInstance(new Object[]{new Integer(1), new Float(1.0f)});


    For this reason if can lock your code to a homemade/stable interface you
    can survive with the simple way. I often make the following design,
    where I add init(...) method to make actual initialization. I then
    simple use classForName and call init method with appropriate params.

    theClass=(MyClass)Class.forName(className).newInstance();
    theClass.init(123, 12.123);
     
    A_Wieminer, Jul 26, 2005
    #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. Miguel De Anda

    ClassLoader or Class.newInstance

    Miguel De Anda, Jul 14, 2003, in forum: Java
    Replies:
    1
    Views:
    844
    John C. Bollinger
    Jul 15, 2003
  2. Rick
    Replies:
    1
    Views:
    1,619
    Lee Fesperman
    Jun 25, 2004
  3. Glenn Robinson

    newInstance and rmi

    Glenn Robinson, Aug 24, 2004, in forum: Java
    Replies:
    0
    Views:
    374
    Glenn Robinson
    Aug 24, 2004
  4. Per Magnus L?vold
    Replies:
    0
    Views:
    1,402
    Per Magnus L?vold
    Nov 15, 2004
  5. Xavier Seneque
    Replies:
    3
    Views:
    3,905
    Xavier Seneque
    Feb 16, 2005
Loading...

Share This Page