Executing code in static initializerblock => ExceptionInInitializerError

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

Executing code in static initializerblock => ExceptionInInitializerError

Post 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???

admin
Site Admin
Posts: 10068
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Executing code in static initializerblock => ExceptionInInitializerError

Post by admin »

It doesn't you are compiling and running the same class. Delete everything and start afresh.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 144 guests