Java - What is the output: static class

Question

What is the output of the following code.

class EnclosingClass {
  private static int i;

  private static void m1() {
    System.out.println(i);
  }

  static void m2() {
    EnclosedClass.accessEnclosingClass();
  }

  static class EnclosedClass {
    static void accessEnclosingClass() {
      i = 1;
      m1();
    }

    void accessEnclosingClass2() {
      m2();
    }
  }
}

public class Main {
  public static void main(String[] args) {
    EnclosingClass.EnclosedClass.accessEnclosingClass();
    EnclosingClass.EnclosedClass ec = new EnclosingClass.EnclosedClass();
    ec.accessEnclosingClass2();
  }
}


Click to view the answer

1
1

Demo

class EnclosingClass {
  private static int i;

  private static void m1() {
    System.out.println(i);//from  ww w .  j ava  2s . c o  m
  }

  static void m2() {
    EnclosedClass.accessEnclosingClass();
  }

  static class EnclosedClass {
    static void accessEnclosingClass() {
      i = 1;
      m1();
    }

    void accessEnclosingClass2() {
      m2();
    }
  }
}

public class Main {
  public static void main(String[] args) {
    EnclosingClass.EnclosedClass.accessEnclosingClass();
    EnclosingClass.EnclosedClass ec = new EnclosingClass.EnclosedClass();
    ec.accessEnclosingClass2();
  }
}

Related Topic