I am new to java.
code :
class class1 {
int x, y;
class1() {
this(12,8);
this.mul();
}
class1(int x, int y){
this.x = x;
this.y = y;
this.mul();
}
void mul() {
System.out.println(x*y);
}
}
public class this1 {
public static void main(String args[]) {
class1 obj1 = new class1();
class1 obj2 = new class1(1, 5);
}
}
When I run this code :
96
96
5
Question: Why is 96 printed twice. Is it that when I create an object with a parameterized constructor, first the control is passed through default constructor and then to the parameterized constructor
this(12,8).new class1()which is theclass1()constructor. That constructor callsthis(12,8)which is theclass(int,int)constructor. Each of those constructors callsmul()which printsx*y.