I have added the below comments to the program in the constructor, but I still misunderstood what the program is doing. Thanks for any help that you can give.
/* Given the following code
What will be the result of compiling and executing
Test class?
A) Point(10, 20);Point(0,20) CORRECT
B) Point(0, 20);Point(10,20)
C) Point(10, 20);Point(10,20) WRONG ANSWER
D) None of the other options */
class Point {
int x;
int y;
void assign(int x, int y) {
x = this.x; // Redundant as the current value of class instance variable which is 0, is assigned to x
this.y = y; // The standard way to initialise param using this keyword
}
public
String toString() {
return "Point(" + x + ", " + y + ")";
}
}
public class _31_Question_This_Keyword_And_Constructors {
public static void main(String[] args) {
Point p1 = new Point();
p1.x = 10;
p1.y = 20;
Point p2 = new Point();
p2.assign(p1.x, p1.y);
System.out.println(p1.toString() + ";" + p2.toString());
}
}