Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!postnews.google.com!l2g2000prg.googlegroups.com!not-for-mail From: lewbloch Newsgroups: comp.lang.java.programmer Subject: Re: Class.forName().newInstance() vs new Date: Wed, 15 Jun 2011 06:58:56 -0700 (PDT) Organization: http://groups.google.com Lines: 33 Message-ID: <4387a10d-472e-4e0e-9b5c-bb7ba4b48cce@l2g2000prg.googlegroups.com> References: <95ho4qFd7cU1@mid.individual.net> <95jod7FbdbU1@mid.individual.net> <1NGdnZX1kJp-VWnQnZ2dnUVZ_oGdnZ2d@earthlink.com> NNTP-Posting-Host: 108.89.33.208 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1308146594 6409 127.0.0.1 (15 Jun 2011 14:03:14 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 15 Jun 2011 14:03:14 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: l2g2000prg.googlegroups.com; posting-host=108.89.33.208; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: ASELCHRU X-HTTP-UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5324 On Jun 12, 7:37=A0am, 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. 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. 'new' is not a wrapper for 'Class.forName()' because while 'forName()' does return a class object, 'new' does not unless the class instantiated actually is 'Class'. Since the constructors for 'Class' are not exposed, that's unlikely. 'newInstance()' is different from 'new' also. It's invoked reflectively, i.e., via runtime mechanisms, not via a compiled constructor invocation as via 'new', and throws different exceptions from the corresponding constructor. It's also semantically limited to no-arg constructors. 'newInstance()' appears in a reflective context which is perforce going to be slower than compiled-in decisions. Such a context will properly appear only when performance is not the overarching concern, nor readability. -- Lew