Reflection: Constructors with Primitive Data Types

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

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;    }  }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may