• 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

Interfac e initialisation rules

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
can someone please explain me the rules or the order for initialising interfaces.
for example, a question from Dan's collection:
interface C {
char w = DD.echo('w');
char x = DD.echo('x');
}
interface D extends C {
char y = DD.echo('y');
char z = DD.echo('z');
char a = DD.echo(w);
}
class DD implements D {
static char echo(char c) {
System.out.print(c);
return c;
}
public static void main (String[] args) {
System.out.print("Main");
DD dd = new DD();
System.out.println(a);
}
}
answer is Mainyzwxww.
no problem with the main first but why is it yzwxww, are the interface's variables get initialised only if required? don't quite get this one...
anyone?
Thanks (again ! )
Shimi
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I summarized it to myself in this sentence while studying Dan's topic exams...
2-If a class implements an interface that contains some variables, at the point that the class will need to use any of the variables that are NOT compile time constants, all the interface variables will be evaluated. Same happens if an interface extends another interface and need to use one of the super interface variables that are NOT compile time constants. If the variable needed is a [B][I]compile time constant[I][B] then the rest of the variables are not evaluated at runtime.
Compile time constants are for instance final variables that are set to a literal:
final char w = 'w';
In this case, the System.out.println(a); line needs the variable a inherited from the interface D and it is not a compile time constant. So the JVM evaluates all the variables as they appear in the declaration of the interface D, and thus it prints yz.
On trying to evaluate the value of a it needs the variable w that is present in the interface C and is not a compile time constant, so it evaluates all the variables of interface C, so it prints wx although it only meeds w it also evaluates x.
Then after getting the value of w, it uses it to evaluate a, so it prints w again, and then use the value of a in the System.out.println(a); and so it prints w once more....
so you have: mainyzwxww
HTH
 
Shimi Avizmil
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clear as crystal !
Thanks mate.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alfred,
Your response was great. I certainly can't add anything to that.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan, it's only what I learnt from your exams and explanation just wrote it with my own flavour
Thanks for all your efforts for helping us
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also:
An initialization of an subinterface does not cause the initialization of a superinterface --unlike classes.
The initialization of a class implementing an interface does not cause the initialization of the interface.
The initialization of an interface happens only when one of the fields declared in it, initialized to a non constant expression, is accessed.
As a consequence of the last: If superInterface declares fields that are inherited by subInterface, and we are accessing them from within the subInterface or a class that implements it; the derived interface will not be initialized, but its base one.
To check it out replace the "a" in the print statement with "w" and the output is "Mainwxw"
[ October 13, 2002: Message edited by: Jose Botella ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic