Re: Class.forName().newInstance() vs new
On Jun 12, 7:37 am, Martin Gregorie <mar...@address-in-sig.invalid>
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
'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,