日期:2014-05-20  浏览次数:20774 次

一道关于ClassLoader的面试题
请看如下代码, 先不要运行, 说出程序会输出什么?

Java code

import java.net.*;

public class Andromeda {
    private static final Andromeda instance = new Andromeda();
    
    private Andromeda(){
        
    }
    
    public static Andromeda getInstance(){
        return instance;
    }
    
    public static void main(String[] args) throws Exception{
        
        URLClassLoader loader1 = new URLClassLoader(new URL[]{new URL("file:///home/root/workspace/andromeda/bin/")}){
            public String toString(){
                return "loader1";
            }
        };
        
        URLClassLoader loader2 = new URLClassLoader(new URL[]{new URL("file:///home/root/workspace/andromeda/bin/")}, 
                ClassLoader.getSystemClassLoader().getParent()){
            public String toString(){
                return "loader2";
            }
        };

        Class clazz1 = loader1.loadClass("Andromeda");
        Class clazz2 = loader2.loadClass("Andromeda");
        
        Andromeda obj1 = (Andromeda)(clazz1.getDeclaredMethod("getInstance", new Class[0]).invoke(null, new Object[0]));
        System.out.println(loader1 + " loads " + obj1);
        
        Andromeda obj2 = (Andromeda)(clazz2.getDeclaredMethod("getInstance", new Class[0]).invoke(null, new Object[0]));
        System.out.println(loader2 + " loads " + obj2);
        
        Andromeda obj3 = Andromeda.getInstance();
        System.out.println(Andromeda.class.getClassLoader() + " loads " + obj3);
    }
}




------解决方案--------------------
探讨
但是两个被加载的Andromeda类的包名称(都是default)和类名称都完全一致, 那它们是同一个Class对象, 还是两个Class对象呢?