I started to learn Java, and I could not understand one of the examples in the book “Thinking in Java”. In this example, the author presents how he states "simple use of the keyword 'this'":
//Leaf.java //simple use of the "this" keyword public class Leaf { int i = 0; Leaf increment() { i++; return this; } void print() { System.out.println("i = " + i); } public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().increment().print(); } }
And when it really works on the code, I can’t understand what the increment() method returns.
This is not a variable i , is this not an object x ? I just do not understand. I tried to change the program to understand it (for example, replace return this with return i or print x instead of i ), but the compiler shows me errors.
source share