Discussion :: Java.lang Class
-
What will be the output of the program?
interface Foo141 { int k = 0; /* Line 3 */ } public class Test141 implements Foo141 { public static void main(String args[]) { int i; Test141 test141 = new Test141(); i = test141.k; /* Line 11 */ i = Test141.k; i = Foo141.k; } }
A.
Compilation fails.
|
B.
Compiles and runs ok.
|
C.
Compiles but throws an Exception at runtime.
|
D.
Compiles but throws a RuntimeException at runtime.
|
Answer : Option B
Explanation :
The variable k on line 3 is an interface constant, it is implicitly public, static, and final. Static variables can be referenced in two ways:
Via a reference to any instance of the class (line 11)
Via the class name (line 12).
Be The First To Comment