devxlogo

Reflection: Constructors with Primitive Data Types

Reflection: Constructors with Primitive Data Types

Question:
How can I dynamically load a class and call its constructor which has a primitive data type as one of its parameters?

Answer:
Using reflection to dynamically instantiate a class whose constructor takes a primitive type argument is done almost the same way as for constructors with Object-derived types. I’ve covered how to invoke constructors using reflection before, so I’ll only cover the details of dealing with primitive types.

One difference is that for the parameter type argument of Class.getConstructor(Class[]), you need to use one of the predefined Class objects that represent primitive types. These are Boolean.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Double.TYPE, Float.TYPE, and Void.TYPE.

Another difference is that when you invoke the constructor, you must use one of the primitive wrapper types (e.g., Integer) to represent the parameter. The wrapper object will be automatically unwrapped by the newInstance() method if necessary. This allows constructors that use one of the wrapper types as an argument (e.g., ConstructMe(Integer)), to also be dynamically invoked through reflection.

The following demonstrates the steps for invoking a constructor that takes an int argument. If you were dynamically loading the class, you would obtain the class reference using Class.forName() rather than by explicitly referencing it as is done in the example.

import java.lang.reflect.*;public final class PrimitiveReflection {  private int __value;  public PrimitiveReflection(int value) {    __value = value;  }  public void print() {    System.out.println(__value);  }  public static final void main(String[] args) {    Constructor constructor;    Method method;    Class[] parameterTypes = new Class[1];    Object[] parameters;     Class clss;    Object obj;    parameterTypes[0] = Integer.TYPE;    clss = PrimitiveReflection.class;    try {      constructor = clss.getConstructor(parameterTypes);    } catch(NoSuchMethodException e) {      e.printStackTrace();      return;    }    // Set up the constructor parameters before creating a new instance    parameters    = new Object[1];    parameters[0] = new Integer(1776);    try {      obj = constructor.newInstance(parameters);    } catch(InstantiationException e) {      e.printStackTrace();      return;    } catch(IllegalAccessException f) {      f.printStackTrace();      return;    } catch(InvocationTargetException g) {      g.printStackTrace();      return;    }    try {      method = obj.getClass().getMethod("print", null);    } catch(NoSuchMethodException e) {      e.printStackTrace();      return;    }    try {      method.invoke(obj, null);    } catch(IllegalAccessException e) {      e.printStackTrace();      return;    } catch(InvocationTargetException f) {      f.printStackTrace();      return;    }  }}

See also  The Benefits of Patient Engagement Solutions for Healthcare Providers
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist