Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

danchisholm.net august09 comprehensive exam4.Q9

 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There seems to be something missing in your code. The variable a in main() is undefined.
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question: Since interface D extends C, why wasn't C initialized first?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I overlooked that.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I couldn't imagine better explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic