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???
Executing code in static initializerblock => ExceptionInInitializerError
Moderator: admin
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Executing code in static initializerblock => ExceptionInInitializerError
It doesn't you are compiling and running the same class. Delete everything and start afresh.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 144 guests