Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 18)
18.
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; 
    } 
}
Compilation fails.
Compiles and runs ok.
Compiles but throws an Exception at runtime.
Compiles but throws a RuntimeException at runtime.
Answer: Option
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).

Discussion:
4 comments Page 1 of 1.

Thirumurugan said:   6 years ago
Does interface can has constant? Explain

Sun said:   8 years ago
I think it's because the static usage of Interfaces is the same as classes.

Urvashi said:   9 years ago
It is local. So we have to initialize it. We can't use it directly.

Noorshid said:   10 years ago
What is about line 13?

Post your comments here:

Your comments will be displayed after verification.