Page 1 of 1

About question com.enthuware.ets.scjp.v6.2.374

Posted: Thu Aug 29, 2013 1:16 pm
by pankaj
Hi,

The code provided in the "Exhibit" section of this question doesn't compile. If you look at the code, at first glance you see that there is an issue with both the interface definitions. The correct answer for this question should be "It will not even compile", but it shows "It will print 1" as the answer. Please clarify.

Thanks.

interface I
{
int i = 1, ii = Test.out("ii", 2);
}
interface J extends I
{
int j = Test.out("j", 3), jj = Test.out("jj", 4);
}
class Test
{
public static void main(String[] args) { System.out.println(J.i); }
public static int out(String s, int i)
{
System.out.println(s + "=" + i);
return i;
}
}

Re: About question com.enthuware.ets.scjp.v6.2.374

Posted: Thu Aug 29, 2013 1:50 pm
by admin
Hi,
I just tried the given code. It works fine.

HTH,
Paul.

Re: About question com.enthuware.ets.scjp.v6.2.374

Posted: Thu Jan 09, 2014 11:08 am
by dayamoon1
This is a quite marginal example,
however if after

Code: Select all

System.out.println(J.i);
one adds

Code: Select all

System.out.println(J.j);	
it prints
j=3
jj=4
3

i.e. after initializing 'j' via a method call it does initialize 'jj' too
which doesn't happen when a variable is initialized with '1' like in case of 'i' and 'ii'.
What JLS does explain this?
Thank you !