先写一个简单的类
package org.test.three;
public class Book {
public Book(){
System.out.println("Here is Book construction.");
}
public static String bookName="mybook";
private int price;
private String author;
private boolean isSale;
static{
System.out.println("Here is Book static area. The bookName is:"+bookName);
}
public void printAllInfo(){
System.out.println("All info is[bookName:"+bookName+",author:"+author+",price:"+price+",isSale:"+isSale+"]");
}
}
测试类
package org.test.three;
/**
* java中Class.forName和ClassLoader都可用来对类进行加载。
* Class.forName除了将类的.class文件加载到jvm中之外,还会对类进行解释,执行类中的static块。
* 而ClassLoader只将.class文件加载到jvm中,不会执行static中的内容,只有在newInstance才会去执行static块。
*
* Class.forName(name, initialize, loader)中参函数initialize也可控制是否加载static块。
* 调用newInstance()方法将调用构造函数,创建类的对象。
*
* @author lushuaiyin
*
*/
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
//测试0
// Book bk0=new Book();
/Class.forName
// try {
// //测试1
Class cc1=Class.forName("org.test.three.Book");
//
// //测试2
Class cc12=Class.forName("org.test.three.Book");
Book bk1=(Book)cc12.newInstance();
bk1.printAllInfo();
//
// //下面同上
@SuppressWarnings("unchecked")
Class<Book> cc13=(Class<Book>) Class.forName("org.test.three.Book");
Book bk2=cc13.newInstance();
bk2.printAllInfo();
//
// /*第二种Class.forName(name, initialize, loader)
// name:加载的类名;initialize是否初始化,即是否执行static代码块和初始化变量值。loader:class loader
// Class.forName("XXX") 等价于 Class.forName("XXX",true,XXX.class.getClassLoader());
// */
// //测试3
Class cc14=Class.forName("org.test.three.Book",true,org.test.three.Book.class.getClassLoader());
//
// //测试4
Class cc15=Class.forName("org.test.three.Book",false,org.test.three.Book.class.getClassLoader());
//
// //测试5
Class cc16=Class.forName("org.test.three.Book",true,org.test.three.Book.class.getClassLoader());
Book bk3=(Book)cc16.newInstance();
bk3.printAllInfo();
//
// //测试6
// Class cc17=Class.forName("org.test.three.Book",false,org.test.three.Book.class.getClassLoader());
// Book bk4=(Book)cc17.newInstance();
// bk4.printAllInfo();
//
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
// catch (InstantiationException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// }
/ClassLoader
try {
//测试7
// Class cc21=ClassLoader.getSystemClassLoader().loadClass("org.test.three.Book");
//测试8
// Class cc22=org.test.three.Book.class.getClassLoader().loadClass("org.test.three.Book");
// //测试9
// Class cc23=ClassLoader.getSystemClassLoader().loadClass("org.test.three.Book");
// Book bk5=(Book)cc23.newInstance();
// bk5.printAllInfo();
//测试10
// Class cc24=Thread.currentThread().getContextClassLoader().loadClass("org.test.three.Book");
// Book bk6=(Book)cc24.newInstance();
// bk6.printAllInfo();
//测试11
Class cc25=Thread.class.newInstance().getContextClassLoader().loadClass("org.test.three.Book");
Book bk7=(Book)cc25.newInstance();
bk7.printAllInfo();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/*
测试0打印
Here is Book static area. The bookName is:mybook
测试1打印
Here is Book static area. The bookName is:mybook
测试2打印
Here is Book static area. The bookName is:mybook
Here is Book construction.
All info is[bookName:mybook,author:null,price:0,isSale:false]
测试3打印
Here is Book static area. The bookName is:mybook
测试4打印
(无输出内容)
测试5打印
Here is Book static area. The bookName is:mybook
Here is Book construction.
All info is[bookName:mybook,author:null,price:0,isSale:false]
测试6打印
Here is Book static area. The bookName is:mybook
Here is Book construction.
All info is[bookName:mybook,author:null,price:0,isSale:false]
测试7打印
(无输出内容)
测试8打印
(无输出内容)
测试9打印
Here is Book static area. The bookName is:mybook
Here is Book construction.
All info is[bookName:mybook,author:null,price:0,isSale:false]
测试10打印
Here is Book static area. The bookName is:mybook
Here is Book construction.
All info is[bookName:mybook,author:null,price:0,isSale:false]
测试11打印
Here is Book static area. The bookName is:mybook
Here is Book construction.
All info is[bookName:mybook,author:null,price:0,isSale:false]
//链接数据库时,会调用下面代码,其实就是加载驱动类,执行其中的static块,注册驱动。
// Class.forName("com.mysql.jdbc.Driver");
static
{
try
{
DriverManager.registerDriver(new Driver());
}
catch(SQLException E)
{
throw new RuntimeException("Can't register driver!");
}
}
// Class.forName("oracle.jdbc.driver.OracleDriver");
static
{
defaultDriver = null;
Timestamp timestamp = Timestamp.valueOf("2000-01-01 00:00:00.0");
try
{
if(defaultDriver == null)
{
defaultDriver = new OracleDriver();
DriverManager.registerDriver(defaultDriver);
}
}
catch(RuntimeException runtimeexception) { }
catch(SQLException sqlexception) { }
}
*/