This Java example shows how to call one constructor from another of the same class using ‘this’ keyword as shown in below program code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// In Java another constructor of the same class can be called // from a constructor using 'this()'. // Note: 'this()' has to be on the first line. public class Foo { private int x; public Foo() { this(1); } public Foo(int x) { this.x = x; } } |