Question discussion

What is the output of the following Java program?public class Main { int x = 10; class Inner { int x = 20; void show(int x) { System.out.pr…

1970Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

public class Main {
    int x = 10;
    class Inner {
        int x = 20;
        void show(int x) {
            System.out.println(x + " " + this.x + " " + Main.this.x);
        }
    }
    public static void main(String[] args) {
        Main m = new Main();
        Main.Inner obj = m.new Inner();
        obj.show(30);
    }
}
  • A30 20 10
  • B10 20 30
  • C30 10 20
  • D20 30 10
Correct answer: A

Explanation

The bare x is the parameter (30), this.x is the Inner field (20) and Main.this.x is the outer instance field (10).

Discussion 0

Nobody has replied yet. Start the discussion!