Using this to access a field
In case a field is shadowed by method or constructor parameter, the field can be accessed using this keyword.
public class Example { public int val; public Example(int val) { this.val = val; } public static void main(String[] args) { Example obj = new Example(10); System.out.println(obj.val); } }
10
Using this to call another constructor of same class
public class Example { public int val; public Example() { this(100); } public Example(int val) { this.val = val; } public static void main(String[] args) { Example obj = new Example(); System.out.println(obj.val); } }
100