Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!postnews.google.com!news2.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 11 Jun 2011 13:01:12 -0500 Date: Sat, 11 Jun 2011 11:01:10 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Class.forName().newInstance() vs new References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <6aadnUFKX6n1MG7QnZ2dnUVZ_uKdnZ2d@earthlink.com> Lines: 50 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.199.66 X-Trace: sv3-3ojKHbpBs4M+lNz0+hW92TqiyUJDlLm5D4DmrquCEgIqTSUNdDf2+gUlAvPq9jZO0tcDW+U6Ifq7vcm!rprVxULmsK8T9rVMzW2nXUySNL7ZnVWU245TBrcJ2FmQo50jVI8Qj9rr+za7B5KSVKxXPLJO2+SF!HbIhU6DHX6V2y5SUf8eWZW2QhtsLNXmfPZ907SjsbxJXrg== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3148 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5209 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