• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface question

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code:
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;
}
}
Prints: j=3 jj=4 3. I don't understand how the jj=4 is also printed. Can anyone enlighten me on this? Thanks.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob,
IMO it appears because the interface j gets loaded. Since all variables in an interface are assumed static then the initialization happens at load time. Since you have requested to print out K.j the class needs to be loaded. Therefore all initialization will take place. Since your init code prints things out you get both j and jj print outs. Then your print goes ahead and prints out the value for you.
Regards,
Manfred.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic