• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another one from Dan

 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 8
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);
}
}

What is the result of attempting to compile and run the above program?
a. Prints: yzwMain
b. Prints: Mainyzw
c. Prints: wxyzwMain
d. Prints: Mainwxyzw
e. Prints: yzwxwwMain
f. Prints: Mainyzwxww
g. Runtime Exception
h. Compiler Error
i. None of the Above

The answer is D here.. i'm wondering why dont the interface variables get initialized unless explicitly called.. becoz interfaces are also implitly static and once they are loaded they should be intialized?? Whats the deal here!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it! Look here for a recent discussion of the same problem.
-Barry
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saniya,
Does the other thread answer your question?
In short, Java tries to save time by only initializing what is needed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic