Possible Duplicate:
Java this.method() vs method()
I’ve been reading some things and doing some tutorials about android java but I still dont understand what “this” means, like in the following code.
View continueButton = this.findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = this.findViewById(R.id.new_button);
newButton.setOnClickListener(this);
Also why is it in this example that a button is not defined with Button but with View, what is the difference?
ps. Great site!! trying to learn java and got ALLOT of answers by searching here!
The
thiskeyword is a reference to the current object. It is used to pass this instance of the object, and more.For example, these two allocations are equal:
Sometimes you have a hidden field you want to access:
Here you assigned the field
awith the value in the parametera.The
thiskeyword works the same way with methods. Again, these two are the same:Finally, say you have a class MyObject that has a method which takes a MyObject parameter:
In this last example you passed a reference of the current object to a static method.
In Android SDK, a
Buttonis a subclass ofView. You can request theButtonas aViewand cast theViewto aButton: