1.A developer wrote java class distributed in a jar file that was installed in an application server. How many versions of this class can potentially exist within one java application:a.always only one
b.one or multiple: this depends on how many classloaders will load this class
c.exactly two: one created by application classoloader and another by system classloader
d.Nothing to do with version2.Can static methods be virtual i.e. invoked polymorphicallya. yes
b. no
c. it depends on the class declaration
d. Wrong Question

解决方案 »

  1.   

    1.这个题目问的有点模糊
    因为java默认有三种类加载器,而应用服务器都有实现自己的类加载器(可能实现多个)
    一般情况类加载器都有一个父类加载器(当然这种要求不是强制的),类加载器加载类时都会
    给父加载器一个机会。
    因为题目本身并说清楚这个类被放到哪个地方,而且没有说清楚应用服务器的类加载器是否
    具有这种层次关系。
    如果满足这种层次关系的话!那么这个类当然只会被加载一次(事实上每个类加载器都可以加载
    同一个类的不同版本,但是父类加载了,子类就不再加载)。
    如果我把这个jar包放到引导类加载路径,它就会被引导类加载器加载。
    如果我把这个jar包放到扩展类加载路径,它就会被扩展类加载器加载。
    如果我把这个jar包放到系统类加载路径(classpath),它就会被系统类加载器加载。
    如果发到web应用服务器的共享目录,比如common下,就会被共享类加载器加载(所有web应用程序共享)
    如果只是放到某个web应用程序的lib目录下,当然就被制定的web应用程序加载器加载器,对其他web应用程序不见。因为这里有一个类加载的优先级,如果多个路径都有这个jar包,那么后面的就会被忽略,因为
    前面已经加载了,这就会造成新版本的jar包没起作用,原因也就是这个。
    不过有一定需要注意的是:如果自己实现类加载器,完全可以不指定父类加载器,这样无论是否
    已经加载了某个类,我都可以强制加载自己的版本!(类是根据全名和类加载器来区别的)
      

  2.   

    in my opinion, question 2 is a wrong question because there's not such a concept of "virtual method" in JAVA, as it is in C++.
      

  3.   

    自己写一个classloader,想加载多找个版本就加载多少个版本
      

  4.   

    of course there isn't a concept of 'virtual'!
    we can just consider the non-final method as virtual method!
    and there isn't a concept of 'polymorphically' with static method that also 
    called method of Class!