Executing code in static initializerblock => ExceptionInInitializerError
Posted: Fri Nov 29, 2013 3:41 pm
Hello,
Just was trying some things out when evaluating my answer to this q. I dit this:
class LearnJava{
static {
char c = 1;
}
static {
switch(c){
case 1: System.out.println("1");
break;
case 2: System.out.println("2");
break;
case 3: System.out.println("3");
break;
default:System.out.println("default");
break;
}
}
public static void main(String args[]){
LearnJava lj = new LearnJava();
}
}
This gives a compiletime error
"java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - illegal start of type"
at line switch(c).
If I change that line to switch(1) everything is ok.
What am I missing here???
Just was trying some things out when evaluating my answer to this q. I dit this:
class LearnJava{
static {
char c = 1;
}
static {
switch(c){
case 1: System.out.println("1");
break;
case 2: System.out.println("2");
break;
case 3: System.out.println("3");
break;
default:System.out.println("default");
break;
}
}
public static void main(String args[]){
LearnJava lj = new LearnJava();
}
}
This gives a compiletime error
"java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - illegal start of type"
at line switch(c).
If I change that line to switch(1) everything is ok.
What am I missing here???