• 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

Question about Interfaces..

 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It prints
j=3
jj=4
3
any ideas how I can follow this code?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
When u refer to a member variable of an Interface, it Initializes all of its
member variables.
Try this:
interface I{
int i = 1, ii = B.out("ii", 2);
}
interface J extends I{
int jj = B.out("jj", 4);
}
interface K extends J{
int k = B.out("k", 5),j = B.out("j", 3);
}
class B{
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;
}
}

And u will get:
k=5
j=3
3
Hope this helps
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This code shows a difference between interfaces and classes:
An interface (I) is not initialized because of the initialization of a subinterface (J).
And a similarity:
An interface (K) is not initialized because of the acces to its inherited fields (j). But only by the access to the declared ones (j in J).
Make them classes and place static modifiers, and you will see that I is initialized but K is not.
Read the whole story in JLS 12.4.1 When the initialization ocurrs
[ July 02, 2003: Message edited by: Jose Botella ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic