What is the output of the following application?
package mypkg1; //ww w .java2 s . co m public class Main { static class Shape {} public static void main(String[] leaves) { int area = 10+5; final class Rectangle extends Shape { // p1 public int getArea() { return area; // p2 } } System.out.print(new Rectangle().getArea()); } }
A.
The code compiles without issue and prints 15, making Option A correct and Option D incorrect.
The main()
method defines a local class Rectangle that correctly extends Shape, a static nested class, making Option B incorrect.
The method getArea()
is permitted to read the variable area, defined in the main()
method, since it is effectively final, having a value of 15 when it is defined.
Option C is also incorrect.