Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Class.forName().newInstance() vs new Date: Sun, 12 Jun 2011 18:17:39 +0200 Lines: 39 Message-ID: <95k754Fsi0U1@mid.individual.net> References: <95ho4qFd7cU1@mid.individual.net> <95jod7FbdbU1@mid.individual.net> <1NGdnZX1kJp-VWnQnZ2dnUVZ_oGdnZ2d@earthlink.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net qhAcUKP2wD8ccc/wlIWbgAt34+YUWzQPA6Gdj2nptGccHqWZI= Cancel-Lock: sha1:ABOXluLc0KpK+w0HPHKM4UEk4Qc= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110516 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5242 On 06/12/2011 04:37 PM, Martin Gregorie wrote: > On Sun, 12 Jun 2011 07:10:07 -0700, Patricia Shanahan wrote: > >> Is there really any program that instantiates so many database managers >> that the difference between newInstance and constructor, and the >> pipeline reloads related to conditional branches, become measurable? >> > Is there actually that much difference? It strikes me that both 'new' and > Class.forName() are doing essentially the same, namely searching the > class path for the required class, loading it into the JVM and > instantiating an object from it. Actually, the code will execute a Class.forName() under the hoods for the version with direct instantiation. There is still a fundamental difference though: the solution explicitly using Class.forName() will provide the name at runtime while the version with the explicit constructor call will have it compiled into the code. Consequently when providing the name at runtime reflection has to be used (which is usually a tad slower). 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. > I also wonder if 'new' might not be > implemented as a wrapper for Class.forName(). It certainly could be done > that way: both return a class object if they are successful and throw an > exception if the class can't be found. Class.forName() only yields a class (if successful) while new SomeClass yields an instance - so new is more like a wrapper around Class.forName().newInstance(). But as I said, there is a fundamental conceptual difference between the two. Kind regards robert