Java Programming - Declarations and Access Control - Discussion

Discussion Forum : Declarations and Access Control - Finding the output (Q.No. 6)
6.
What will be the output of the program?
interface Count 
{
    short counter = 0;
    void countUp();
}
public class TestCount implements Count 
{
    public static void main(String [] args) 
    {
        TestCount t = new TestCount();
        t.countUp();
    }
    public void countUp() 
    {
        for (int x = 6; x>counter; x--, ++counter) /* Line 14 */
        {
            System.out.print(" " + counter);
        }
    }
}
0 1 2
1 2 3
0 1 2 3
1 2 3 4
Compilation fails
Answer: Option
Explanation:

The code will not compile because the variable counter is an interface variable that is by default final static. The compiler will complain at line 14 when the code attempts to increment counter.

Discussion:
1 comments Page 1 of 1.

Swetha said:   1 decade ago
Interface variables are by default they are public final and static its like constant.
(1)

Post your comments here:

Your comments will be displayed after verification.