Java - What is the output: static field

Question

What is the output?

public class Main {
  static int a;

  void printA() {
    System.out.println(a);
    System.out.println(this.a);
  }

  static void hi() {
    System.out.println("hi");
  }

  public static void main(String[] args) {
    System.out.println(a);
    System.out.println(Main.a);
    System.out.println(new Main().a);

    new Main().printA();

    hi();
    new Main().hi();
  }
}


Click to view the answer

0
0
0
0
0
hi
hi

Demo

public class Main {
  static int a;

  void printA() {
    System.out.println(a);/*from w w  w  .  j  a  va 2 s  .  c  o  m*/
    System.out.println(this.a);
  }

  static void hi() {
    System.out.println("hi");
  }

  public static void main(String[] args) {
    System.out.println(a);
    System.out.println(Main.a);
    System.out.println(new Main().a);

    new Main().printA();

    hi();
    new Main().hi();
  }
}

Related Topic