Groups | Search | Server Info | Login | Register


Groups > comp.lang.java.programmer > #5242

Re: Class.forName().newInstance() vs new

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Class.forName().newInstance() vs new
Date 2011-06-12 18:17 +0200
Message-ID <95k754Fsi0U1@mid.individual.net> (permalink)
References (1 earlier) <95ho4qFd7cU1@mid.individual.net> <dKudnSwyLLvWM27QnZ2dnUVZ_o2dnZ2d@earthlink.com> <95jod7FbdbU1@mid.individual.net> <1NGdnZX1kJp-VWnQnZ2dnUVZ_oGdnZ2d@earthlink.com> <it2ivj$qqe$1@localhost.localdomain>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Class.forName().newInstance() vs new Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 22:43 +0530
  Re: Class.forName().newInstance() vs new David Lamb <dalamb@cs.queensu.ca> - 2011-06-11 13:46 -0400
  Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-11 19:49 +0200
    Re: Class.forName().newInstance() vs new Patricia Shanahan <pats@acm.org> - 2011-06-11 11:04 -0700
      Re: Class.forName().newInstance() vs new Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 23:52 +0530
      Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 14:05 +0200
        Re: Class.forName().newInstance() vs new Patricia Shanahan <pats@acm.org> - 2011-06-12 07:10 -0700
          Re: Class.forName().newInstance() vs new Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-12 14:37 +0000
            Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 18:17 +0200
              Re: Class.forName().newInstance() vs new Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-12 15:25 -0400
            Re: Class.forName().newInstance() vs new lewbloch <lewbloch@gmail.com> - 2011-06-15 06:58 -0700
              Re: Class.forName().newInstance() vs new Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-06-16 10:16 +0100
          Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 18:10 +0200
    Re: Class.forName().newInstance() vs new Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 23:53 +0530
      Re: Class.forName().newInstance() vs new "John B. Matthews" <nospam@nospam.invalid> - 2011-06-11 15:43 -0400
        Re: Class.forName().newInstance() vs new "John B. Matthews" <nospam@nospam.invalid> - 2011-06-11 15:50 -0400
          Re: Class.forName().newInstance() vs new lewbloch <lewbloch@gmail.com> - 2011-06-15 06:49 -0700
            Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-16 11:21 -0400
              Re: Class.forName().newInstance() vs new lewbloch <lewbloch@gmail.com> - 2011-06-17 06:45 -0700
                Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-17 12:29 -0400
                Re: Class.forName().newInstance() vs new Lewis Bloch <lewisbloch@google.com> - 2011-06-17 11:07 -0700
                Re: Class.forName().newInstance() vs new "John B. Matthews" <nospam@nospam.invalid> - 2011-06-18 02:01 -0400
                Re: Class.forName().newInstance() vs new Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-18 10:19 -0300
                Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-18 20:16 +0200
                OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-20 19:19 +0000
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-20 20:46 +0000
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-21 20:42 +0000
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Gene Wirchenko <genew@ocis.net> - 2011-06-21 14:22 -0700
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-20 18:55 -0300
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Tom Anderson <twic@urchin.earth.li> - 2011-06-21 23:29 +0100
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-21 20:39 -0300
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) "John B. Matthews" <nospam@nospam.invalid> - 2011-06-21 23:00 -0400
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Tom Anderson <twic@urchin.earth.li> - 2011-06-22 20:02 +0100
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-22 20:03 +0000
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Michael Wojcik <mwojcik@newsguy.com> - 2011-06-23 12:04 -0400
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Michael Wojcik <mwojcik@newsguy.com> - 2011-06-23 11:58 -0400
                Re: Class.forName().newInstance() vs new Gene Wirchenko <genew@ocis.net> - 2011-06-20 14:01 -0700
                Re: Class.forName().newInstance() vs new Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-20 18:53 -0300
                Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-21 19:53 -0400
                Re: Class.forName().newInstance() vs new blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-22 20:01 +0000
                Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-23 12:16 -0400
                OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-20 19:18 +0000
                Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-20 20:52 +0000
        Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 14:14 +0200
  Re: Class.forName().newInstance() vs new Patricia Shanahan <pats@acm.org> - 2011-06-11 11:01 -0700
  Re: Class.forName().newInstance() vs new Roedy Green <see_website@mindprod.com.invalid> - 2011-06-13 06:16 -0700
  Re: Class.forName().newInstance() vs new Ian Shef <invalid@avoiding.spam> - 2011-06-14 20:07 +0000

csiph-web