Java - What is the output: for variables scope

Question

What is the output of the following code.

public class Main {
  static int b = 2;
  int c = 3;
  final static int d = 7;

  public static void main(String[] args) {

    shadow(0, 100, 700);
    new Main().shadow(200, 300);
  }

  private static void shadow(int a, int b, int d) {
    System.out.println(b);
    System.out.println(Main.b);
    System.out.println(d);
  }

  public void shadow(int b, int c) {
    System.out.println(b);
    System.out.println(Main.b);
    System.out.println(c);
    System.out.println(this.c);
  }
}


Click to view the answer

100
2
700
200
2
300
3

Demo

public class Main {
  static int b = 2;
  int c = 3;//from ww  w . j ava  2 s .c  o m
  final static int d = 7;

  public static void main(String[] args) {

    shadow(0, 100, 700);
    new Main().shadow(200, 300);
  }

  private static void shadow(int a, int b, int d) {
    System.out.println(b);
    System.out.println(Main.b);
    System.out.println(d);
  }

  public void shadow(int b, int c) {
    System.out.println(b);
    System.out.println(Main.b);
    System.out.println(c);
    System.out.println(this.c);
  }
}

Related Quiz