wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes danchisholm.net august09 comprehensive exam4.Q9 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "danchisholm.net august09 comprehensive exam4.Q9" Watch "danchisholm.net august09 comprehensive exam4.Q9" New topic
Author

danchisholm.net august09 comprehensive exam4.Q9

Dmitry Golynkin
Greenhorn

Joined: Aug 13, 2002
Posts: 23
code:
__________________________________________________
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);
}
}
__________________________________________________
Why "w" is printed two times?


good luck
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
There seems to be something missing in your code. The variable a in main() is undefined.
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
output is Mainyzwxww
yz--> initialize Interface D
wx--> initialize Interface C
ww--> static echo char method in class DD
System.out.println('w');
return ('w');

hope this helps,
Maria


SCJP 1.4
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It compiles and runs fine for me, though I'm as confused as you are about the order that the strings are printed.
"a" is a variable in interface D, which class DD implements.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
My question: Since interface D extends C, why wasn't C initialized first?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi there:
The output that I got is:
Mainyzwxww
1. First the "Main" is printed.
2. Then object dd is created, its instance var
are initialized as follows:
a. char y = DD.echo('y');
This statement calls echo() that prints y and
returns 'y' whis is assigned to y.

b. char z = DD.echo('z');
This statement calls echo() that prints z and
returns 'z' which is assgined to z.
c. char a = DD.echo(w);
This statement calls echo() that calls
following statement to get value of w:

char w = DD.echo('w');
This statement calls echo() that prints w and
returns 'w' which is assigned to w.
As one variable is needed from interface D, the rest
will be initialized at the same time (per Dan's explaination)
So following statement is executed:

char x = DD.echo('x');
which prints x and assigns 'x' to x.
After that w in following statement recievs its value:
char a = DD.echo(w);
which calls echo() and prints w and
assigns 'w' to a.
Now initialization is finished.
3. Now following statement is executed:
System.out.println(a);
The value of a is known so it is printed i.e. 'w'.
I understood this after reading Dan's explaination that
if one var needs initialization in an interface rest are
initialized at the same time.
Hope this helps.
Barkat
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Originally posted by Anthony:
There seems to be something missing in your code. The variable a in main() is undefined.
The member variables inherited from an interface
are automatically final and static. Therefore
a direct reference to var a is allowed in static
main.
Hope this helps.
Barkat
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Thanks, I overlooked that.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Ron Newman:
My question: Since interface D extends C, why wasn't C initialized first?


The following quote is an excerpt from the
Java Language Specification, Section 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.

The following quote describes when initialization occurs.
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

T is a class and an instance of T is created.

T is a class and a static method declared by T is invoked.

A static field declared by T is assigned.

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.

Invocation of certain reflective methods in class Class and in package java.lang.reflect also causes class or interface initialization. A class or interface will not be initialized under any other circumstance.


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

Joined: Aug 05, 2002
Posts: 787
Hi Dan:
I scan through the link you provided above. It
not specifically talk about interfac var
initialization.
Bruce Eckel in Thinking in Java specifically says
that interface var are automatically static and
final. Whenever, one of the interface var is
accessed, the class (interface to be exact) is loaded and all interface
var are initialized.
Thanks
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

Since interface D extends C, why wasn't C initialized first?

Interesting question!
You know that the reason why classes are initialized from top to botton is that a call to a super constructor -super(...)- must be placed as the first instruction in the body of some constructor in the derived class.
However interfaces haven't constructors. How are interfaces initialized? But, first, what is meant by initializing an interface?
The compiler gathers the code that initializes the static final fields via non compile constant expressions, and places it within a method called clinit. The CLass INITializer method of an interface only initializes fields with non constant expressions. The compiler gathers this code in textual order.
The initialization of a subinterface doesn't cause the initialization of the superinterface. Interfaces are initialized when one of its members are accessed.
D is initialized because we are accessing a from main. C is initialized because we are accessing w from D. Try replacing "DD.echo(w);" with "DD.echo('w');" and you won't see the output wx.
Running "javap -c C" the clinit method, named static by javap, is:

and for D:


SCJP2. Please Indent your code using UBB Code
Dmitry Golynkin
Greenhorn

Joined: Aug 13, 2002
Posts: 23
Thank you. I couldn't imagine better explanation
 
 
subject: danchisholm.net august09 comprehensive exam4.Q9
 
Similar Threads
interface and initialization
Another one from Dan
Interfac e initialisation rules
a doubt on interfaces & classes
Mock Exam Dan - Initialization A (Topic)