Java Programming - Java.lang Class - Discussion

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); 
    } 
}

[A]. 1
[B]. 2
[C]. 3
[D]. Compilation Fails

Answer: Option D

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.


Rui said: (Dec 26, 2010)  
It's not just that. It is also not possible to refer to instance methods in a static context :)

Victor Anica said: (Jul 16, 2012)  
Exactly; for example we rename the troubling method to "test()", then the following message will occur when compiling : cannot make a static reference to the non-static reference test() from the type <className>.

Sandeep Gujjar said: (Jul 23, 2013)  
Hi friends I want to know what is logical difference b/w || and |?

Iroshan said: (May 25, 2014)  
Actually explanation is ok. But not fully right.

Another reason is to display the compilation error is.

- non static methods can not be referenced in static context. Main method is static.

Yogesh Prabhukhanolkar said: (Jun 18, 2014)  
Its not about just that only.

First line of main makes x=0;

x is instance variable, it can't be accessed directly, first line itself fails!

Arpit said: (Sep 24, 2014)  
If it would not a compilation error then what would be the answer.

Daniel said: (Sep 28, 2014)  
Another thing is catch is reserved keyword, so it won't compile.

Biswa said: (Dec 20, 2014)  
Even if you change the method name to catch1, it will still not compile since its not declared static.

Raaaaaaaa said: (Jul 23, 2016)  
How it works? please explain.

public class NewClass
{
static int x;

static boolean catc()
{
x++;

return true;
}
public static void main(String[] args)
{

if ((catc() |catc()) || catc())
{
x++;
System.out.println(x);
}
}
}

The output is 3.

Anil said: (Apr 2, 2017)  
Replay for given question
public class NewClass
{
static int x;

static boolean catc()
{
x++;

return true;
}
public static void main(String[] args)
{

if ((catc() |catc()) || catc())
{
x++;
System.out.println(x);
}
}
}
---------------------------
@Raaaaaaaa
The output of the given question is 3 because,

if(catc()|catc())// call for two times so x value is become 2 (bit wise( |) will perform first it will convert into binary then perform OR gate operation)
and the (catc()|catc()) give output is true so next condition will not check so other catc() will not execute.

* inside the if condition there is statement x++ so it will increase the value of x by 1 so in next line sop(x)=>3.

Anshu said: (Sep 10, 2017)  
Is that not necessary to assign a value to a static variable as soon as it is declared?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.