Get Class given its fully qualified name
suggest changeGiven a String containing the binary name of a class, its Class object can be loaded with Class.forName. The one-argument form initializes the class and uses the caller's class loader:
Class clazz = null;
try {
clazz = Class.forName("java.lang.Integer");
} catch (ClassNotFoundException ex) {
throw new IllegalStateException(ex);
}
The three-argument form chooses whether the class is initialized and which ClassLoader loads it. A null loader means the bootstrap class loader. Primitive names such as "int" are not accepted; Class.forName("int") throws ClassNotFoundException.
ClassLoader classLoader = ...
boolean initialize = ...
Class clazz = null;
try {
clazz = Class.forName("java.lang.Integer", initialize, classLoader);
} catch (ClassNotFoundException ex) {
throw new IllegalStateException(ex);
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents