aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface and initialization 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 "interface and initialization" Watch "interface and initialization" New topic
Author

interface and initialization

Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
In Mock Exam(Dan Chisholm),

answer is b.
The output for this program have wx,not yza.why?


Val SCJP2 1.2<br />====>SCJD
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The key here can be found (as usual) in the JLS. This time, from §12.4 Initialization of Classes and Interfaces:

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


SCJP Tipline, etc.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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>
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
Corey,Thank you for giving me explanation.
Dan Chisholm,Thank you for giving me good Mock Exam.
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
i ran the program and indeed it outputs, mainwxw.
how come it doesn't output 'y' and 'z'. im confused


SCJP 1.4
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
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.
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104

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)?
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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?
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
oh ok, got it.
i read the java language specification and it really helped. thanks guys!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: interface and initialization