Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.java.programmer > #5209
| Date | 2011-06-11 11:01 -0700 |
|---|---|
| From | Patricia Shanahan <pats@acm.org> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Class.forName().newInstance() vs new |
| References | <it07on$ilc$1@speranza.aioe.org> |
| Message-ID | <6aadnUFKX6n1MG7QnZ2dnUVZ_uKdnZ2d@earthlink.com> (permalink) |
On 6/11/2011 10:13 AM, Abu Yahya wrote:
> I have a class that instantiates a private member based on a value in a
> configuration file.
>
> The value is used to determine the class name.
>
> I initially decided to use code similar to the following:
>
>
> // class name comes from property file
> String dbMgrClassName = props.getProperty("db.manager");
> Class<?> k = Class.forName(dbMgrClassName);
> db = (DB) k.newInstance();
> db.init(props);
>
> However, a colleague pointed out that using "new" is faster than using
> "Class.forName().newInstance()". So, I changed the code to the following:
>
> String dbMgrClassName = props.getProperty("db.manager");
> if (dbMgrClassName.equals("DB2"))
> db = new DB2();
> else if (dbMgrClassName.equals("Oracle"))
> db = new Oracle();
> else if (dbMgrClassName.equals("SQLServer"))
> db = new SQLServer();
> else { // handle unrecognized
> Class<?> k = Class.forName(dbMgrClassName);
> db = (DB) k.newInstance();
> }
> db.init(props);
>
> My question is: Does using if-else statements like the above really
> improve performance? What is the best approach to instantiate classes
> whose type you don't know at compile time?
Is performance really a critical issue here? Instantiating a database
manager is usually done quite infrequently, normally once per run, and
is often followed by opening a database, a relatively heavyweight operation.
I could see a case for the second version if you wanted to control the
options for the implementing class. Given the decision to preserve the
old code to handle unrecognized class names it does not even have that
advantage. In order to properly test the second version you will need an
additional class, not "DB2", "Oracle", or "SQLServer", so that the
fall-through code gets exercised.
The first version is simpler and easier to test, so it wins unless it
makes a significant difference in overall program performance.
Patricia
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Class.forName().newInstance() vs new Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 22:43 +0530
Re: Class.forName().newInstance() vs new David Lamb <dalamb@cs.queensu.ca> - 2011-06-11 13:46 -0400
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-11 19:49 +0200
Re: Class.forName().newInstance() vs new Patricia Shanahan <pats@acm.org> - 2011-06-11 11:04 -0700
Re: Class.forName().newInstance() vs new Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 23:52 +0530
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 14:05 +0200
Re: Class.forName().newInstance() vs new Patricia Shanahan <pats@acm.org> - 2011-06-12 07:10 -0700
Re: Class.forName().newInstance() vs new Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-12 14:37 +0000
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 18:17 +0200
Re: Class.forName().newInstance() vs new Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-12 15:25 -0400
Re: Class.forName().newInstance() vs new lewbloch <lewbloch@gmail.com> - 2011-06-15 06:58 -0700
Re: Class.forName().newInstance() vs new Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-06-16 10:16 +0100
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 18:10 +0200
Re: Class.forName().newInstance() vs new Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 23:53 +0530
Re: Class.forName().newInstance() vs new "John B. Matthews" <nospam@nospam.invalid> - 2011-06-11 15:43 -0400
Re: Class.forName().newInstance() vs new "John B. Matthews" <nospam@nospam.invalid> - 2011-06-11 15:50 -0400
Re: Class.forName().newInstance() vs new lewbloch <lewbloch@gmail.com> - 2011-06-15 06:49 -0700
Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-16 11:21 -0400
Re: Class.forName().newInstance() vs new lewbloch <lewbloch@gmail.com> - 2011-06-17 06:45 -0700
Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-17 12:29 -0400
Re: Class.forName().newInstance() vs new Lewis Bloch <lewisbloch@google.com> - 2011-06-17 11:07 -0700
Re: Class.forName().newInstance() vs new "John B. Matthews" <nospam@nospam.invalid> - 2011-06-18 02:01 -0400
Re: Class.forName().newInstance() vs new Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-18 10:19 -0300
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-18 20:16 +0200
OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-20 19:19 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-20 20:46 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-21 20:42 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Gene Wirchenko <genew@ocis.net> - 2011-06-21 14:22 -0700
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-20 18:55 -0300
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Tom Anderson <twic@urchin.earth.li> - 2011-06-21 23:29 +0100
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-21 20:39 -0300
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) "John B. Matthews" <nospam@nospam.invalid> - 2011-06-21 23:00 -0400
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Tom Anderson <twic@urchin.earth.li> - 2011-06-22 20:02 +0100
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-22 20:03 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Michael Wojcik <mwojcik@newsguy.com> - 2011-06-23 12:04 -0400
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Michael Wojcik <mwojcik@newsguy.com> - 2011-06-23 11:58 -0400
Re: Class.forName().newInstance() vs new Gene Wirchenko <genew@ocis.net> - 2011-06-20 14:01 -0700
Re: Class.forName().newInstance() vs new Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-20 18:53 -0300
Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-21 19:53 -0400
Re: Class.forName().newInstance() vs new blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-22 20:01 +0000
Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-23 12:16 -0400
OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-20 19:18 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-20 20:52 +0000
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 14:14 +0200
Re: Class.forName().newInstance() vs new Patricia Shanahan <pats@acm.org> - 2011-06-11 11:01 -0700
Re: Class.forName().newInstance() vs new Roedy Green <see_website@mindprod.com.invalid> - 2011-06-13 06:16 -0700
Re: Class.forName().newInstance() vs new Ian Shef <invalid@avoiding.spam> - 2011-06-14 20:07 +0000
csiph-web