1.java static inner class 和 non-static inner class的區(qū)別?
2.請(qǐng)寫(xiě)出一個(gè)singleton模式的class.
你如果寫(xiě)出下面的2種樣式,我會(huì)問(wèn)你:請(qǐng)問(wèn)你如何在同一個(gè)jvm中并且在同一個(gè)classLoader中得到它的多個(gè)實(shí)例?(請(qǐng)不要奇怪)
樣列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體內(nèi),或者方法的聲明沒(méi)有throws,那么編譯是通不過(guò)的.
ok,請(qǐng)看如下的代碼:
public class TestClass {
public void testMethod()/*這里沒(méi)有throws 哦!*/{
......
throw new Exception("force throw the exception...");
......
}
}
很明顯上面的方法如果這樣的話是通不過(guò)編譯的,但是如果非得要你在testMethod體中在運(yùn)行時(shí)throw一個(gè)很一般的Exception,請(qǐng)問(wèn)你有辦法嗎?
這3道題可不是sun出的考題哦!不信你搜搜......