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
Exceptions
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 .
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){}
}