Before a class is initialized, its superclass must be initialized, but interfaces implemented by the class are not initialized. Similarly, the superinterfaces of an interface are not initialized before the interface is initialized.
Therefore, the interfaces are not automatically intialized when a new instance of DD is created. However, the JLS goes on to explain when initialization does occur:
A class or interface type T will be initialized immediately before the first occurrence of any one of the following: ... A static field declared by T is used and the reference to the field is not a compile-time constant (�15.28). References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.
Therefore, when a reference is made to the variable w, in the interface C, that interface is initialized. At no point, however, does interface D get initialized because no references are made to any members of that interface. I hope that helps, Corey
Corey, Thank you for giving Val that quick answer. I think that I'll have to improve the explanation that is associated with the answer to that question. Val is not the first person to ask for help. I have also received email from others. My next version of the exam will have a link to the JLS at the bottom of each answer page. For those that still need help (or a drink), I'll also add a link back here to the saloon.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Joined: Nov 27, 2001
Corey,Thank you for giving me explanation. Dan Chisholm,Thank you for giving me good Mock Exam.
Is it that all the statements in a interface gets executed when the control goes to it ??? if above statment is yes... then maria, sop(w) looks for char w =DD.echo('w'); in the immediate interface D,it is not there in interface D,then looks in interface high in order which is C. all the stat here gets executed by calling echo() in DD, last control comes to again sop(w) which prints w. try adding more similar stat's in C, u get all the stat's executed before the control returns to DD.,if char w =DD.echo('w'); is there in interface D, and then it would not go and looks higher up, see progrom below which is slightly modified.
please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Joined: Dec 20, 2001
The key is that interface D never gets initialized. Therefore, the static initializers (the invocations of the echo method) never get executed for the members of that interface. Please re-read my previous post as that explains why that interface never gets executed. If you're still confused, let me know. Corey
Joined: Jan 23, 2001
hi, what is illegal fwd reference, when i comment char w = DD.echo('w'); in interface C., in the 1st post(val lee) program, then i get compile error: illegal forward reference char w = DD.echo(w); ^ thanks in advance.
when i comment char w = DD.echo('w'); in interface C., in the 1st post(val lee) program, then i get compile error: illegal forward reference char w = DD.echo(w);
srinivas, commenting out the line 1 will result in compiler error ,not recognizing the variable w in the rest of the program. is the compiler error in char a=DD.echo(w) or char w=DD.echo(w)?
Joined: Jul 02, 2002
Originally posted by Corey McGlone: The key is that interface D never gets initialized. Therefore, the static initializers (the invocations of the echo method) never get executed for the members of that interface. Please re-read my previous post as that explains why that interface never gets executed. If you're still confused, let me know. Corey
I agree with Corey: his previous post and JLS Section 12.4 Initialization of Classes and Interfaces provides the complete explanation. Section 12.4.1 provides the answer for this next question that I will put into the next version of my mock exam. Are there any non-certified forum members that would like to explan this one?
Joined: Jul 14, 2002
oh ok, got it. i read the java language specification and it really helped. thanks guys!