| Author |
Class.forName() vs ClassLoader.loadClass()
|
srinivas srinivasmeenavalli
Ranch Hand
Joined: Jul 13, 2008
Posts: 65
|
|
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() ?
?
|
 |
john sal
Ranch Hand
Joined: Jul 30, 2010
Posts: 92
|
|
|
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.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Class.forName() vs ClassLoader.loadClass()
|
|
|