1.java static inner class 和 non-static inner class的區別?
2.請寫出一個singleton模式的class.
你如果寫出下面的2種樣式,我會問你:請問你如何在同一個jvm中并且在同一個classLoader中得到它的多個實例?(請不要奇怪)
樣列1:
public class Singleton {
private final static Singleton instance=new Singleton();
private Singleton(){}
public static Singleton newInstance(){
return instance;
}
}
樣列2:
public class Singleton {
private static volatile int instanceCounter=0;
private Singleton(){
if(instanceCounter>0)
throw new RuntimeException("can't create multi instances!");
instanceCounter++;
}
private final static Singleton instance=new Singleton();
public static Singleton newInstance(){
return instance;
}
}
3.java 的exception 分checked,unchecked.像RuntimeException,Error都不用顯式try-catch,直接可以throw,
但是一般的exception是必須catch的:
throw new Exception("..."),如果這句不在try-catch體內,或者方法的聲明沒有throws,那么編譯是通不過的.
ok,請看如下的代碼:
public class TestClass {
public void testMethod()/*這里沒有throws 哦!*/{
......
throw new Exception("force throw the exception...");
......
}
}
很明顯上面的方法如果這樣的話是通不過編譯的,但是如果非得要你在testMethod體中在運行時throw一個很一般的Exception,請問你有辦法嗎?
這3道題可不是sun出的考題哦!不信你搜搜......