您的位置:首页 > Web前端

Difference Between Class.forName() and ClassLoader.loadClass()

2016-12-22 21:36 330 查看
文章转载至 http://javabeat.net/class-forname-classloader-loadclass-difference/

Class.forName() and ClassLoader.loadClass(),both the classes dynamically loading the classes
to the classpath. However, there are subtle difference on initializing the classes at the
time of loading and from where it is loaded.This tutorials compares both the form of class
loading with simple examples.
1.Class.forName()
-By default the classes are initialized at the time of loading. It means that static
variables in the classes are initialized.
-Also the class is loaded from the current class loader. When you invoke the
Class.forName for loading the JDBC driver class, it is loaded to the
same class loader from where it is invoked. In short, it is loaded to
the caller’s class loader.
-Class.forName is overloaded method. Invoking with single string parameter is
equivalent of Class.forName(className, true, currentLoader). Optionally
you can pass the second and third parameters to change the behavior.
className – Fully qualified name of the class to be loaded
initialize – Whether to initialize the class or not. By default the value
is “true”classLoader – By default the value is current class loader.
Optionally you can change the class loader name.
2.ClassLoader.loadClass()
-By default, the classes are not initialized. The classes are loaded and made
available in the classpath, the variables are initialized only when it is
first time invoked by the caller.
-Another advantage of this class is that you can load the classes to any specific
class loader. Which may or may not be
the loader that loads that calling code. If picking a specific loader to
load the class is important to your design,you should ClassLoader.loadClass().
3.Class Loading Example
TestClass.java
package javabeat.net.corejava;
public class TestClass {
static {
System.out.println("Static Initializer Called!!");
}
}
ClassLoadingExample.java
package javabeat.net.corejava;
public class ClassLoadingExample {
public static void main(String[] args) {
try {
System.out.println("Before Loading the forName");
Class.forName("javabeat.net.corejava.TestClass");
System.out.println("After Loading the forName");
ClassLoader.getSystemClassLoader().loadClass("javabeat.net.corejava.TestClass");
System.out.println("After Loading the loadClass");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
Output
Before Loading the forName
Static Initializer Called!!
After Loading the forName
After Loading the loadClass
4.ClassNotFoundException
If the class is not found in the classpath, you would encounter the following exception.
java.lang.ClassNotFoundException: javabeat.net.xml.TestClass1
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at javabeat.net.corejava.ClassLoadingExample.main(ClassLoadingExample.java:8)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: