We can load a class by using either Class.forName() or ClassLoader.loadClass() dynamically.What is the general practise in Java ?
is it Class.forName() ?
Joined: Jul 30, 2010
Both are same however ClassLoader is a abstract class that specifially deals with the the loading the class dynamically and contains method that can be overridden in the subclasses to extend the functionality of class loading.
They aren't exactly the same; there are subtle differences. Class.forName(String) uses the current class loader, whereas ClassLoader.loadClass() uses the ClassLoader the method is invoked on. The latter is more similar to Class.forName(String, boolean, ClassLoader), but the latter lets you choose whether or not to initialize the class or not.
Unless you need to use a specific ClassLoader or the control over initialization, Class.forName(String) is just fine.