By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Creating instances by reflection - how expensive?

P: n/a
Hi All, I was wondering what peoples opinions, or experience were with
creating instances by reflection. How expensive is the following code? It is
from a factory class, I was wondering whether to create a cache of the
instances when first created & then return clones.

Command returnCommand = null;
String requestedCommand = request.getPathInfo();
if(requestedCommand != null){
try {
Class commandClass = Class.forName(requestedCommand);
Class[] parameterTypes = new Class[0];
Constructor commandConstructor =
commandClass.getConstructor(parameterTypes);
Object[] initArgs = new Object[0];
returnCommand = (Command)
commandConstructor.newInstance(initArgs);
}
catch (ClassNotFoundException ....etc etc

Thanks :-)
--
-P
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
That's one of those things where you'll just have to Try It And See(TM).
Run some benchmarks on different VMs and operating environments.
I'm sure they'll come in handy to yourself or someone else one day - post
them to a web site even.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
Home : +61 7 5502 7987
Work : +61 7 5552 4076
Mobile : 0408 711 099
(2003 VTR1000F)
"PerfectDayToChaseTornados" <pd***@emailaddress.invalid> wrote in message
news:Z9*************@news-binary.blueyonder.co.uk...
Hi All, I was wondering what peoples opinions, or experience were with
creating instances by reflection. How expensive is the following code? It is from a factory class, I was wondering whether to create a cache of the
instances when first created & then return clones.

Command returnCommand = null;
String requestedCommand = request.getPathInfo();
if(requestedCommand != null){
try {
Class commandClass = Class.forName(requestedCommand);
Class[] parameterTypes = new Class[0];
Constructor commandConstructor =
commandClass.getConstructor(parameterTypes);
Object[] initArgs = new Object[0];
returnCommand = (Command)
commandConstructor.newInstance(initArgs);
}
catch (ClassNotFoundException ....etc etc

Thanks :-)
--
-P

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.