• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hurting My Brains

 
Chris Cairns
Ranch Hand
Posts: 48
  • 0
  • 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;
}
}
It will print j = 3, jj=4 and then 3.
Could someone explain to me in laymen's terms the reason for the output in the above code. I'm really confused over the difference between compile-time constants and initializations with respect to the above code.
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is initialization of a class??? it is when the class members (specially fields) will be initialized and static initializers will be executed. Some fields (the final compile time constant ones) are inlined by the java compiler, this does not require any class initialization.
final int x=2;
/*2 is a final compile time constant any reference to it will be replaced by value 2*/
final int y=m();
/* this is an example of a final field that isn't a compile time constant the compiler will not inline variable y and if the virtual machine finds any reference to this field, m() will be called once only, it's result will be assigned to y. This is an example of initialization*/

final fields that are compile time constants are inlined at compile time. If you use any final field of any class or instance in your code, the compiler (at compile time, as i said) will replace your reference with the value of the final variable. That's why you have this results while attempting to execute the code from JLS.
 
Chris Cairns
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the jj variable printed out?
 
sharana sharana
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is hidden in the following definition
"A reference to a class field causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.
"
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(J.i);
System.out.println(K.j);
}
static int out(String s, int i) {
System.out.println(s + "=" + i);
return i;
}
}

The reference to J.i is to a field that is a compile-time constant; therefore, it does not cause I to be initialized and so i is printed first which is 1. 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 and jj so they are printed 3 and 4,
now when k.j is referenced in main j is actually declared in interface j and it already been intialized and hence k.j prints 3 again
hence the output 1,3,4,3
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic