Java - What is the output: Enclosing class

Question

What is the output of the following code.

class Enclosing {
  void m(final int x) {
    final int y = x*2;

    class Enclosed {
      int a = x;
      int b = y;
    }

    Enclosed ec = new Enclosed();
    System.out.println(ec.a);
    System.out.println(ec.b);
  }
}

public class Main {
  public static void main(String[] args) {
    Enclosing ec = new Enclosing();
    ec.m(10);
    System.out.println(Math.abs(-1));
  }
}


Click to view the answer

10
20
1

Demo

class Enclosing {
  void m(final int x) {
    final int y = x*2;

    class Enclosed {
      int a = x;//from   w ww .ja  v a 2  s. c o m
      int b = y;
    }

    Enclosed ec = new Enclosed();
    System.out.println(ec.a);
    System.out.println(ec.b);
  }
}

public class Main {
  public static void main(String[] args) {
    Enclosing ec = new Enclosing();
    ec.m(10);
    System.out.println(Math.abs(-1));
  }
}

Related Topic