Sometimes you get strange questions in interviews, you should always expect the unexpected. ;)
Any technique you use has positives and minuses. You have to know when is a good time to use them and what the problem might be and how to work around them.
The important thing to remember that almost every problem has a solution or a work around.
On my last interview I was asked a standard question about all Singleton implementations in java. And how bad they all are.
Sounds less like a question, more like a religious debate.
And I was told that even the static initialization is bad because of the probability of unchecked exception in constructor:
Siderman IV: Spider man vs Singleton and the Static Initialiser. (the bad guys ;)
throw new RuntimeException("Wow... Exception in Singleton constructor...");
That is a bad idea, so don't do that. In fact it won't even compile. The compiler in Java 6 reports.
initializer must be able to complete normally
And they also told me that the Exception is gonna be "ClassNotFoundException" so it would be extremely hard to find the problem in real app.
You get a ExceptionInInitializerError with a cause of the exception which caused the problem. Apart from having to read the nested exception its not that tricky.
static class Inner {
static {
Integer.parseInt(null);
}
}
public static void main(String... args) {
try {
new Inner();
} catch (Throwable e) {
e.printStackTrace();
}
new Inner();
}
prints
Exception in thread "main" java.lang.ExceptionInInitializerError
at Main.main(Main.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at Main$Inner.<clinit>(Main.java:8)
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class Main$Inner
at Main.main(Main.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
This happens the first time, after that you get NoClassDefFoundError if you try to use the class again.
It used to be that when you tried to access a class using reflections, and it failed you got a ClassNotFoundException (with no details as to what really happens) However this is no longer the case.