Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 25)
25.
What will be the output of the program?
public class ExamQuestion6 
{
    static int x; 
    boolean catch()
    {
        x++; 
        return true; 
    } 
    public static void main(String[] args)
    {
        x=0; 
        if ((catch() | catch()) || catch()) 
            x++; 
        System.out.println(x); 
    } 
}
1
2
3
Compilation Fails
Answer: Option
Explanation:

Initially this looks like a question about the logical and logical shortcut operators "|" and "||" but on closer inspection it should be noticed that the name of the boolean method in this code is "catch". "catch" is a reserved keyword in the Java language and cannot be used as a method name. Hence Compilation will fail.

Discussion:
11 comments Page 2 of 2.

Rui said:   1 decade ago
It's not just that. It is also not possible to refer to instance methods in a static context :)


Post your comments here:

Your comments will be displayed after verification.