Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization Related Prob

 
Shalini Banerjee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
interface K extends J
{
int k = Test.out("k", 5);
}
class Test
{
public static void main(String[] args) { System.out.println(K.j); }
public static int out(String s, int i)
{
System.out.println(s + "=" + i);
return i;
}
}

why the answer is,
j=3
jj=4
3


please explain.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi shalini,

you wont get such exams in the exam. this question has been posted previously on javaranch.

the exma tests you on how better you understand the objectives and not on the runtime and compile time behaviour of a code containinig interfaces

so ignore this code
 
Shalini Banerjee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...thnx for your suggesion.
But still i want the explanation from somebody who can explain it.
 
Chandrakanth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reference to K.j is a reference to a field actually declared in interface J that is not a compile-time constant, this causes initialization of the fields of interface J, but not those of its superinterface I, nor those of interface K. Despite the fact that the name K is used to refer to field j of interface J, interface K is not initialized.

you can refer to this link to know more on this:JLS
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic