Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news.netcologne.de!newsfeed-fusi2.netcologne.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade Newsgroups: comp.lang.java.programmer Subject: Re: Class.forName().newInstance() vs new Date: Thu, 16 Jun 2011 10:16:55 +0100 Lines: 19 Message-ID: <95u008FrdgU1@mid.individual.net> References: <95ho4qFd7cU1@mid.individual.net> <95jod7FbdbU1@mid.individual.net> <1NGdnZX1kJp-VWnQnZ2dnUVZ_oGdnZ2d@earthlink.com> <4387a10d-472e-4e0e-9b5c-bb7ba4b48cce@l2g2000prg.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: individual.net 51VqR7BuaWXneEveTPkA3wmcLq+fw6h/bbqKZhm/FD4a4dsk0O Cancel-Lock: sha1:Zwm//II4rWbNn6aYqQxR4cVdHO4= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.1.16) Gecko/20101125 SUSE/3.0.11 Thunderbird/3.0.11 In-Reply-To: <4387a10d-472e-4e0e-9b5c-bb7ba4b48cce@l2g2000prg.googlegroups.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5329 On 15/06/11 14:58, lewbloch wrote: > > '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. > There is an alternative to the "simple" Class.newInstance() which allows constructors with arguments to be used. The Class.getConstructor(Class...) or Class.getDeclaredConstructor(Class...) method can be used to obtain other constructors for the class. The newInstance(Object) method of the returned Constructor can then be used to get an instance of the class (where obj is actually an Object[] containing the constructor arguments). -- Nigel Wade