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

文章转载至 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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值