I have this code copied from Android developers website:
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
...
}
I am wondering what exactly "this" keyword refers to? Does it refer to the class "ExampleActivity"? And in general how to find what "this" refers to?
Yes, 'this' refers to the instance of the enclosing class.
Reference (from the Sun Java Tutorial):
"this" is a reference to the current object.
In your case, it refers to an instance of the ExampleActivity class.
http://download.oracle.com/javase/tutorial/java/javaOO/thiskey.html
this
refers to the most inner class instance. In your example it refers toExampleActivity
which is of typeOnClickListener
which is passed in tosetOnClickListener
.It refers to the instance of
ExampleActivity
on whichonCreate()
has been called.In general, from the Java Language Specification, 15.8.3: