Class.forName(xxx.xx.xx) 返回的是一個(gè)類,作用是要求JVM查找并加載指定的類。由于類與靜態(tài)代碼是綁定的,因此也會(huì)執(zhí)行靜態(tài)代碼段。但是沒有實(shí)例化。例如
輸出A static
如果把toRun.newInstance();這句話的注釋去掉,輸出
A static
init A
Class.forName與newInstance() 相當(dāng)于把Class A=new A()分為了兩步: 先加載這個(gè)類,然后再實(shí)例化
不同之處是:
Class.forName可以使代碼的耦合度降低
但是newInstance只能調(diào)用無(wú)參構(gòu)造函數(shù)。但是new可以調(diào)用有參構(gòu)造函數(shù)
輸出
A static
B static
輸出
Echo arg0 = Echo
Echo arg1 = a1
Echo arg2 = a2
Echo arg3 = a3
import java.lang.reflect.Method;
public class DynamicLoader {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Class toRun = Class.forName(args[0]);
//toRun.newInstance();
//Method mainMethod = finMain(toRun);
//mainMethod.invoke(null, new Object[]{args});
}
private static Method finMain(Class toRun){
Method[] methods = toRun.getMethods();
for(Method method : methods){
if(method.getName().equals("main")){
return method;
}
}
return null;
}
}
class Echo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<args.length;i++){
System.out.println("Echo arg"+i+" = "+args[i]);
}
}
}
class A{
public A(){
System.out.println("init A");
}
static{
System.out.println("A static");
}
}
輸入java DynamicLoader A a1 a2 a3public class DynamicLoader {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Class toRun = Class.forName(args[0]);
//toRun.newInstance();
//Method mainMethod = finMain(toRun);
//mainMethod.invoke(null, new Object[]{args});
}
private static Method finMain(Class toRun){
Method[] methods = toRun.getMethods();
for(Method method : methods){
if(method.getName().equals("main")){
return method;
}
}
return null;
}
}
class Echo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<args.length;i++){
System.out.println("Echo arg"+i+" = "+args[i]);
}
}
}
class A{
public A(){
System.out.println("init A");
}
static{
System.out.println("A static");
}
}
輸出A static
如果把toRun.newInstance();這句話的注釋去掉,輸出
A static
init A
Class.forName與newInstance() 相當(dāng)于把Class A=new A()分為了兩步: 先加載這個(gè)類,然后再實(shí)例化
不同之處是:
Class.forName可以使代碼的耦合度降低
但是newInstance只能調(diào)用無(wú)參構(gòu)造函數(shù)。但是new可以調(diào)用有參構(gòu)造函數(shù)
import java.lang.reflect.Method;
public class DynamicLoader {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Class toRun = Class.forName(args[0]);
//toRun.newInstance();
//Method mainMethod = finMain(toRun);
//mainMethod.invoke(null, new Object[]{args});
}
private static Method finMain(Class toRun){
Method[] methods = toRun.getMethods();
for(Method method : methods){
if(method.getName().equals("main")){
return method;
}
}
return null;
}
}
class Echo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<args.length;i++){
System.out.println("Echo arg"+i+" = "+args[i]);
}
}
}
class A{
public A(){
System.out.println("init A");
}
static{
System.out.println("A static");
}
}
class B extends A{
public B(){
System.out.println("init B");
}
static{
System.out.println("B static");
}
}
輸入java DynamicLoader B a1 a2 a3public class DynamicLoader {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Class toRun = Class.forName(args[0]);
//toRun.newInstance();
//Method mainMethod = finMain(toRun);
//mainMethod.invoke(null, new Object[]{args});
}
private static Method finMain(Class toRun){
Method[] methods = toRun.getMethods();
for(Method method : methods){
if(method.getName().equals("main")){
return method;
}
}
return null;
}
}
class Echo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<args.length;i++){
System.out.println("Echo arg"+i+" = "+args[i]);
}
}
}
class A{
public A(){
System.out.println("init A");
}
static{
System.out.println("A static");
}
}
class B extends A{
public B(){
System.out.println("init B");
}
static{
System.out.println("B static");
}
}
輸出
A static
B static
import java.lang.reflect.Method;
public class DynamicLoader {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Class toRun = Class.forName(args[0]);
toRun.newInstance();
Method mainMethod = finMain(toRun);
mainMethod.invoke(null, new Object[]{args});
}
private static Method finMain(Class toRun){
Method[] methods = toRun.getMethods();
for(Method method : methods){
if(method.getName().equals("main")){
return method;
}
}
return null;
}
}
class Echo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<args.length;i++){
System.out.println("Echo arg"+i+" = "+args[i]);
}
}
}
class A{
public A(){
System.out.println("init A");
}
static{
System.out.println("A static");
}
}
輸入java DynamicLoader Echo a1 a2 a3public class DynamicLoader {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Class toRun = Class.forName(args[0]);
toRun.newInstance();
Method mainMethod = finMain(toRun);
mainMethod.invoke(null, new Object[]{args});
}
private static Method finMain(Class toRun){
Method[] methods = toRun.getMethods();
for(Method method : methods){
if(method.getName().equals("main")){
return method;
}
}
return null;
}
}
class Echo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<args.length;i++){
System.out.println("Echo arg"+i+" = "+args[i]);
}
}
}
class A{
public A(){
System.out.println("init A");
}
static{
System.out.println("A static");
}
}
輸出
Echo arg0 = Echo
Echo arg1 = a1
Echo arg2 = a2
Echo arg3 = a3