wood burning stoves 2.0*
The moose likes Java in General and the fly likes Class.newInstance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class.newInstance" Watch "Class.newInstance" New topic
Author

Class.newInstance

Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Hi,

I get a list of class names in an array and need to load them dynamically

One way is to use:

Class c= Class.forName(arr[]i]);
c.newInstance();

My concern is performance here will be the performance?

Do I have any other options to load these clasess.

Thanks
Ahmad


OCP/MCP/SCJP/SCWCD/IBM XML/SCMAD/SCEA-1
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Class.forName will cause the class to be loaded (if it is not already). You do not have to use newInstance if it is only going to create an instance you're never going to use. If you do use, all good.

If you tell the difference between Cllass.forName() and loading the class directly I'd be very surprised, and since it only occurs once for the life of the application it really shouldn't have any impact on performance except at the moment they're all loaded.

Dave
Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Thanks David,

Basically the requirement is to read a header from JMS message and based on this create the correct object.

The number of JMS message can be very high?

Is this the right approach to handle this scenario with Class.newInstance?

The objects will be used.

Thanks
Ahmad
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Your current code creates the new object alright, but immediately throws it away again.
You're going to have to assign it to something to do anything useful with it.


42
Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

These objects are used to pass data between the front end and back end. and object names are dymanic as passed by some jms header .

They are basically ValueObjects
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
*If* you have a fixed set of classes, you could put Abstract Factories into a Set, with the classnames as keys.

But I doubt that the performance difference is significant. I would only try it if the other way *proved* to be too slow.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Originally posted by Ahmad Khan:
These objects are used to pass data between the front end and back end. and object names are dymanic as passed by some jms header .

They are basically ValueObjects



Loading through Class.forName will be performed only once as I see you in your application or requirement, so as I think you will not get any performance problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.newInstance