newInstance method (format 2)

Description

Directly creates the instances of the class indicated by the parameter type in the Explicit memory block. The value specified in the parameter args is passed as an argument of the constructor that creates the instances. The objects created through the initialization by the constructor of the instances of the class specified in the parameter are created in the Java heap.

Format

public Object newInstance(Class type,  Object... args);

Parameters

type:
This is the class of the array instance to be created directly.
args:
This is a parameter that is passed to the constructor.

Exceptions

NullPointerException:
The value of either one of the parameters or both the parameters type and args is null.
SecurityException:
This exception is thrown when SecurityManager exists, and when any of the following conditions hold true:
  • The invocation of s.checkMemberAccess(type, Member.PUBLIC) does not allow access to this constructor.
  • The class loader at the calling side is different.
  • The invocation of the class loader is higher than the current class loader, and also the invocation of s.checkPackageAccess() does not allow access to the package of this class.
NoSuchMethodException:
A public constructor having a parameter of the same type as the elements of the parameter args does not exist in the class indicated by the parameter type.
ExceptionInInitializerError:
An attempt to initialize the parameter type or the class indicated by the parameter type has failed.
InstantiationException:
The parameter type or the class indicated by the parameter type is either an abstract class or an interface.
InvocationTargetException:
An exception occurred during the execution of the parameter type or the constructor of the class indicated by the parameter type.
InaccessibleMemoryAreaException:
This functionality is not supported.
IllegalAccessException:
The base constructor cannot be accessed because Java language access control is executed in the Constructor object.
IllegalArgumentException:
This exception is thrown in each of the conditions:
  • When the number of real parameters and virtual parameters is different
  • When the lap release conversion of the primitive arguments fails
  • When the parameter value cannot be converted to the corresponding virtual parameter type after lap release of the primitive argument
  • When the constructor is related to the enumeration type

Return value

This method returns the reference to the instances created in the Explicit memory block indicated by the object.

If it is judged that processing cannot be executed after performing the common error check, acquire the java.lang.reflect.Constructor instance as type.getConstructor(arg_types#). In this case, consider java.lang.reflect.Constructor as a receiver, parameter args as a parameter, invoke the java.lang.reflect.Constructor.newInstance(Object... initargs) method, and return the result. For details on common error check, see 10.6 Error check (common error check) of the process that controls the Explicit memory block .

#
arg_types is a class array in which the results of invocation of Object.getClass() as the object are assumed as the elements of the parameter args.

Caution

We recommend that you add a public class in the parameter type.

You cannot invoke a constructor in which the primitive type is assumed as an argument. To invoke a constructor in which the primitive type is assumed as an argument, use the newInstance method (format 3). A coding example in which the newInstance method (format 3) is used is as follows:

import JP.co.Hitachi.soft.jvm.MemoryArea.*;
import java.lang.reflect.*;
public class test1 {
 public static void main(String[] args) throws Exception {
   ExplicitMemory em = new BasicExplicitMemory();
   TheClass obj = null;

   Constructor cons = TheClass.class.getConstructor(new Class[]{int.class});
   obj = (TheClass)em.newInstance(cons, 1);            // Execution successful

   obj = (TheClass)em.newInstance(TheClass.class, 1);  // NoSuchMethodException is thrown
 }
}

public class TheClass {
 public TheClass(int i){}
}