共用方式為


Class.NewInstance Method

Definition

Creates a new instance of the class represented by this Class object.

[Android.Runtime.Register("newInstance", "()Ljava/lang/Object;", "")]
public Java.Lang.Object NewInstance ();
[<Android.Runtime.Register("newInstance", "()Ljava/lang/Object;", "")>]
member this.NewInstance : unit -> Java.Lang.Object

Returns

a newly allocated instance of the class represented by this object.

Attributes

Exceptions

if the default constructor is not visible.

if the instance cannot be created.

Remarks

Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.

This member is deprecated. This method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The java.lang.reflect.Constructor#newInstance(java.lang.Object...) Constructor.newInstance method avoids this problem by wrapping any exception thrown by the constructor in a (checked) java.lang.reflect.InvocationTargetException.

<p>The call

<pre>clazz.newInstance()</pre>

can be replaced by

<pre>clazz.getDeclaredConstructor().newInstance()</pre>

The latter sequence of calls is inferred to be able to throw the additional exception types InvocationTargetException and NoSuchMethodException. Both of these exception types are subclasses of ReflectiveOperationException.

Java documentation for java.lang.Class.newInstance().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to