************************
首先我创建一个类:
package wcd.test.classforname;
public class TestClass_forName {
String i = "";
//静态代码块
static{
System.out.println("静态代码块执行了");
}
public static void go(){
System.out.println("static方法执行了");
}
public void hello(){
System.out.println("普通方法执行了");
}
public TestClass_forName(){
System.out.println("无参构造执行了");
}
public TestClass_forName(String i){
this.i=i;
}
}
***********************
然后在另一个类里调用
1.Class c = Class.forName("wcd.test.classforname.TestClass_forName");
2.Class c2 = TestClass_forName.class;
3.TestClass_forName t1 = new TestClass_forName();
4.TestClass_forName.class.newInstance();
5.Class.forName("wcd.test.classforname.TestClass_forName").newInstance();
1与2的执行效果一样,只执行静态代码块
3与4与5先执行静态代码块,后执行无参构造。
首先我创建一个类:
package wcd.test.classforname;
public class TestClass_forName {
String i = "";
//静态代码块
static{
System.out.println("静态代码块执行了");
}
public static void go(){
System.out.println("static方法执行了");
}
public void hello(){
System.out.println("普通方法执行了");
}
public TestClass_forName(){
System.out.println("无参构造执行了");
}
public TestClass_forName(String i){
this.i=i;
}
}
***********************
然后在另一个类里调用
1.Class c = Class.forName("wcd.test.classforname.TestClass_forName");
2.Class c2 = TestClass_forName.class;
3.TestClass_forName t1 = new TestClass_forName();
4.TestClass_forName.class.newInstance();
5.Class.forName("wcd.test.classforname.TestClass_forName").newInstance();
1与2的执行效果一样,只执行静态代码块
3与4与5先执行静态代码块,后执行无参构造。