This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.newInstance

 
Aryan Khan
Ranch Hand
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aryan Khan
Ranch Hand
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*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.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic