• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help  RSS feed

 
Balaji Parthasarathy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
Please look at the following code,

and output is:
1
j=3
jj=4
3
I am not getting this processing.Hope somebody will explain in detail.
Thanks
Balaji H P

(edited by Cindy to format code using [ code] and [ /code] tags without the spaces)
[ March 11, 2003: Message edited by: Cindy Glass ]
 
William Barnes
Ranch Hand
Posts: 1049
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly would you like this code to do?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing that happens is
System.out.println(J.i);
which is causes the J interface to be loaded, which causes the I interface to be loaded. Variable i is inherited by J and the primitive value 1 is printed. Notice that no methods from the interfaces had to be called to get this done. Also notice that 1 is a compile time constant. So this step can be done without acutally initializing the Interface.
Then you asked for K.j which uses a method to init the value. Well, to get that the Interfaces have to be initialized also. So it is trying to do
int j = Test.out("j", 3);
which prints the j=3 from the initialization step and returns the int value 3 to the calling method. But when the J Interface is initialized, it does the entire interface, not just the one variable that it needs. So jj is also initialized which causes the
jj=4 to be printed. Finally the main method gets back the 3 that was returned from calling the j method and the value 3 is printed.
See JLS 12.4 Initialization of Classes and Interfaces
and Balu,
Please change your name to be compliant with JavaRanch's naming policy. It should not be obviously fictitious.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy
[ March 11, 2003: Message edited by: Cindy Glass ]
 
William Barnes
Ranch Hand
Posts: 1049
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See, now wasn't my answer a lot better?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!