aspose file tools
The moose likes Java in General and the fly likes Class.forName() vs ClassLoader.loadClass() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Class.forName() vs ClassLoader.loadClass()" Watch "Class.forName() vs ClassLoader.loadClass()" New topic
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Class.forName() vs ClassLoader.loadClass()
 
Similar Threads
How to Load Serialized Plugins?
Dynamic class loading
Classnotfoundexception class.forName()
Problem with dynamically setting the classpath at runtime
[newbie] IllegalAccessException