Consider the following 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(); }//w w w.jav a 2 s .co m 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): Calls to super()
or this()
should be the first statement in a constructor, hence both the calls cannot be there in a constructor.
Option d): Recursive constructor invocation for Point()
that the compiler would detect.
Option e): You cannot refer to an instance field x while explicitly invoking a constructor using this keyword.