Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

Sending parameters using newInstance()?

Thread view: 
frank - 26 Jul 2005 02:16 GMT
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
Patricia Shanahan - 26 Jul 2005 02:41 GMT
> Was wondering when using something like this
> theClass= (myClass)Class.forName(className).newInstance();
[quoted text clipped - 6 lines]
>
> 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 - 26 Jul 2005 02:47 GMT
Thanks would you happen to have an example?

>> Was wondering when using something like this
>> theClass= (myClass)Class.forName(className).newInstance();
[quoted text clipped - 14 lines]
>
> Patricia
Adam Maass - 26 Jul 2005 03:20 GMT
>>> Was wondering when using something like this
>>> theClass= (myClass)Class.forName(className).newInstance();
[quoted text clipped - 16 lines]
>
> 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)});
A_Wieminer - 26 Jul 2005 11:20 GMT
>>>>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 {
[quoted text clipped - 4 lines]
> 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);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.