Given:
class Line {
public class Point {
public int x, y;
}
public Point getPoint() {
return new Point();
}
}
class Triangle {
public Triangle() {
// insert code here
}
}
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
A.
Point p = Line.getPoint();
B.
Line.Point p = Line.getPoint();
C.
Point p = (new Line()).getPoint();
D.
Line.Point p = (new Line()).getPoint();
Explanation:
A:
Main.java:13: cannot find symbol
symbol : class Point
location: class Triangle
Point p = Line.getPoint();
^
Main.java:13: non-static method getPoint() cannot be referenced from a static context
Point p = Line.getPoint();
^
2 errorsB:
Main.java:13: non-static method getPoint() cannot be referenced from a static context
Line.Point p = Line.getPoint();
^
1 errorC:
Main.java:13: cannot find symbol
symbol : class Point
location: class Triangle
Point p = (new Line()).getPoint();
^
1 errorD:
The program compiled successfully
Option D is the correct answer. Option A is false because class “Point” will not be recognized by itself, it must be referenced using its father’s name before. Option B is false because method getPoint from class “Line” can’t be called statically. Option C is false because the same reason of option A.
D