Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1023 :
Posted: Tue Feb 21, 2017 5:55 am
by caromahal
Doesn't in point 2:
public class X{
static int k = 0;
static {
k= 10/0;
}}
throw an ArithmeticException ?
This exception is not available in the set of Possible solutions
Re: About Question enthuware.ocajp.i.v8.2.1023 :
Posted: Tue Feb 21, 2017 9:53 am
by BlietZ
I believe all errors/exceptions in the initializer will throw a ExceptionInInitializerError.
edit: see this post
viewtopic.php?f=2&t=2222
Re: About Question enthuware.ocajp.i.v8.2.1023 :
Posted: Fri Aug 04, 2017 12:27 am
by shambhavi
I would like to know why the following shows a compiler error ?
static
{
throw new NullPointerException();
}
but the below code doesn't .
static
{
if(true)
throw new NullPointerException();
}
if(true) evaluates to true always is known at compile time itself right ? Then why does it compile ? Whereas the first case doesn't?
Re: About Question enthuware.ocajp.i.v8.2.1023 :
Posted: Fri Aug 04, 2017 12:48 am
by admin
The if(true) construct is a special case that is exempted from compilation error by the specification to allow conditional compilation. There are many variations of the same question.
Please see this for details:
https://docs.oracle.com/javase/specs/jl ... l#d5e19555
Re: About Question enthuware.ocajp.i.v8.2.1023 :
Posted: Fri Aug 04, 2017 12:58 am
by shambhavi
thanks

Re: About Question enthuware.ocajp.i.v8.2.1023 :
Posted: Thu Jan 25, 2018 11:59 am
by kevin35
Code: Select all
// #1
public static void main(String[] args) {
int x;
final boolean DEBUG=true;
if (DEBUG) {
x = 1;
return;
}
System.out.println(x); //compile error: The local variable x may not have been initialized
}
Code: Select all
// #2
public static void main(String[] args) {
int x;
if (true) {
x = 1;
return;
}
System.out.println(x); //compiles fine
}
Hi Paul, can you please explain what is the difference between #1 and #2?
DEBUG is a compile time constant, so why #1 does not compile?
Re: About Question enthuware.ocajp.i.v8.2.1023 :
Posted: Thu Jan 25, 2018 10:07 pm
by admin
There should not be any issue for either one. Both compile fine for me.
Re: About Question enthuware.ocajp.i.v8.2.1023 :
Posted: Fri Jan 26, 2018 2:43 am
by kevin35
okay i tried to compile it with command prompt and it works.
wth is eclipse doing?
(I know you shouldn't use eclipse for the exam, I used command prompt for 2 weeks while learning for the exam then it was to annoying and I switched back to IDE)