+ 2

Where to use "this" keyword in java?

I am getting confuse where to use "this" keyword in program

27th Aug 2018, 5:35 PM
Devang Patel
Devang Patel - avatar
2 odpowiedzi
+ 4
Whenever you call 'this' you are handling the object calling the method. In Java almost everything is an object, so you will be calling the method of an object, on an object. To get the reference of the calling object in the method you use 'this'. It's also useful to distinguish code when an ambiguity might be present. for instance: public void set(int param) { this.param = param; } left side of equals refers to the class variable, right side refers to the local method parameter
27th Aug 2018, 6:47 PM
Dan Walker
Dan Walker - avatar
+ 2
In short, this means - if you put this in MainActivity class that means, MainActivity.this (that class itself)..
27th Aug 2018, 5:59 PM
jamilxt
jamilxt - avatar