Table of Contents

Java - Reflection (java.lang.Class)

About

Language - Reflection in Java

API

The java.lang.Class:

Usage

Disadvantage

Snippet

// Example a class in the same package
classToInstantiate = Class.forName(myClass.class.getPackage().getName()+"."+"AName");

// Build an instance Constructor
Class[] constructorClassType = {TheReturnedInstantiateType.class};
Constructor constructor = classToInstantiate.getConstructor(constructorClassType );

// Instantiate a new instance
Object[] constructorParamValue = {The value that match the class constructor};
constructor.newInstance(constructorParamValue);

Documentation / Reference