• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface and initialization

 
Val Lee
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Mock Exam(Dan Chisholm),

answer is b.
The output for this program have wx,not yza.why?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Val Lee
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey,Thank you for giving me explanation.
Dan Chisholm,Thank you for giving me good Mock Exam.
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i ran the program and indeed it outputs, mainwxw.
how come it doesn't output 'y' and 'z'. im confused
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok, got it.
i read the java language specification and it really helped. thanks guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic