Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Joshua Cranmer Newsgroups: comp.lang.java.programmer Subject: Re: Class.forName().newInstance() vs new Date: Sun, 12 Jun 2011 15:25:10 -0400 Organization: A noiseless patient Spider Lines: 16 Message-ID: References: <95ho4qFd7cU1@mid.individual.net> <95jod7FbdbU1@mid.individual.net> <1NGdnZX1kJp-VWnQnZ2dnUVZ_oGdnZ2d@earthlink.com> <95k754Fsi0U1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 12 Jun 2011 19:25:11 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="bAymlyY9SkaJNa8Tz2rerw"; logging-data="725"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/ltfMAlhhnTZd1r39fQVhV5AcBjJ6b0A=" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.16pre) Gecko/20110305 Lightning/1.0b3pre Thunderbird/3.1.10pre In-Reply-To: <95k754Fsi0U1@mid.individual.net> Cancel-Lock: sha1:IsrLUccbl0zfcxX2gnXlrElk4hY= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5274 On 06/12/2011 12:17 PM, Robert Klemme wrote: > The conceptual difference between providing a type at compile time and > run time is reflected by the necessity to use reflection (in the dynamic > case) which defers particular type checks to runtime - which is usually > significantly slower when done frequently because it has to be done for > _every_ call. One particular difference is that, as new uses a class already in the constant pool, the lookup only needs to be done at most once per file, since it is otherwise indexed by an integer into a cache. Class.forName will have a slightly more expensive lookup, since it needs to index a hashtable by a String. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth