wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfac e initialisation rules Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfac e initialisation rules" Watch "Interfac e initialisation rules" New topic

Interfac e initialisation rules

Shimi Avizmil

Joined: Oct 09, 2002
Posts: 16
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) {
return c;
public static void main (String[] args) {
DD dd = new DD();
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...
Thanks (again ! )
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
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

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Shimi Avizmil

Joined: Oct 09, 2002
Posts: 16
Clear as crystal !
Thanks mate.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Your response was great. I certainly can't add anything to that.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link: http://aspose.com/file-tools
subject: Interfac e initialisation rules
It's not a secret anymore!