Given this class definition:
class Point { private int x = 0, y; public Point(int x, int y) { this.x = x; this.y = y; } // DEFAULT_CTOR }
Which one of the following definitions of the Point constructor can be replaced without compiler errors in place of the comment DEFAULT_CTOR
?
a) public Point() { this(0, 0); super();//from w w w . ja v a 2 s . c om } b) public Point() { super(); this(0, 0); } c) private Point() { this(0, 0); } d) public Point() { this(); } e) public Point() { this(x, 0); }
c)
options a) and b) Both the calls super()
and this()
cannot be provided in a constructor.
option d) the call this()
; will result in a recursive constructor invocation for Point()
constructor (and hence the compiler will issue an error).
option e) You cannot refer to an instance field x while explicitly invoking a constructor using this keyword.