Page 1 of 1

Executing code in static initializerblock => ExceptionInInitializerError

Posted: Fri Nov 29, 2013 3:41 pm
by javaman
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???

Re: Executing code in static initializerblock => ExceptionInInitializerError

Posted: Sat Nov 30, 2013 3:56 am
by admin
It doesn't you are compiling and running the same class. Delete everything and start afresh.

HTH,
Paul.