-2

What does return this statement do in this line? I've Googled a lot but found no relevant answers regarding this :( There is a mistake in the setA method but that is intentional.

E show() {
    return this;
}

Here is the full code, it is working fine.

public class E{ 
int a;
public int getA() {
    return a;
}
public void setA(int a) {
    a = a;
}
E show() {
    return this; //What is the role of this line?
}
public static void main(String[] args) {
    E obj = new E();
    obj.setA(10);
    System.out.println(obj.getA());
    E obj2 = obj.show();
    System.out.println(obj2.getA());        
}
}

Here is the output

0
0

1 Answers1

0

ob.show() will return the same instance of the object to which obj is having a reference to. Now obj and obj2 will be having a reference to the same object. You can modify your object using any reference varible and the changes will be reflected both sides .