Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Sending parameters using newInstance()?

Reply
Thread Tools

Sending parameters using newInstance()?

 
 
frank
Guest
Posts: n/a
 
      07-26-2005
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

 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-26-2005
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
 
Reply With Quote
 
 
 
 
frank
Guest
Posts: n/a
 
      07-26-2005
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


 
Reply With Quote
 
Adam Maass
Guest
Posts: n/a
 
      07-26-2005

"frank" <(E-Mail Removed)> 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)});


 
Reply With Quote
 
A_Wieminer
Guest
Posts: n/a
 
      07-26-2005
>>>>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).newInst ance();
theClass.init(123, 12.123);
 
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
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
pls help me when i sent mail, it vil sending twice instead of once ,am using java.mail,am sending my code.... shailajabtech@gmail.com Java 0 09-28-2006 06:38 AM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
does a "parameters"-parameter overwrite the "parameters"-object? Florian Loitsch Javascript 11 03-15-2005 03:33 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments