Java Variable Scope method variable shadow class field variable

Question

What is the output of the following code?


public class Main {
  private static int x = 1;

  public static void main(String[] args) {
    int x = 5;//from  w  w  w .  j av a 2 s . c o  m

    System.out.println(x);

    a();
    b();
    a();
    b();

    System.out.println(x);
  }

  public static void a() {
    int x = 25;

    System.out.println(x);
    ++x;
    System.out.println(x);
  }

  public static void b() {
    System.out.println(x);
    x *= 10; // modifies class Scope's field x
    System.out.println(x);
  }
}


5
25
26
1
10
25
26
10
100
5



PreviousNext

Related