I've read hundreds of explanations on "this" in java and I'm really having trouble grasping it. I'm learning android and java side-by-side, I know it's harder that way but I'm enjoying it. The one thing I'm getting killed on is "this"... I'm pasting code from a tutorial below that utilizes "this" one time. I was going to just put a piece of the code but want to be as helpful as possible.
I'm looking for a good explanation of "this" that I can add to my notes. Any and all help is appreciated. Thanks in advance.
example code starts below:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import android.view.View;
import android.content.DialogInterface;
import android.app.Dialog;
import android.app.AlertDialog;
public class DialogActivity extends Activity {
CharSequence[] items = { "Google", "Apple", "Microsoft" };
boolean[] itemsChecked = new boolean [items.length];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View v) {
showDialog(0);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("This is a dialog with some simple text...")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(),
"OK Clicked!", Toast.LENGTH_SHORT).show();
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(),
"Cancel clicked!", Toast.LENGTH_SHORT).show();
}
}
)
.setMultiChoiceItems(items, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
Toast.makeText(getBaseContext(),
items[which] + (isChecked ? " checked!":" unchecked!"),
Toast.LENGTH_SHORT).show();
}
}
).create();
}
return null;
}
}
Think of this
as "itself". If you pass this
to a method, you're simply passing an instance of the object to the method.
ie: Student
is an object, as is Classroom
. If I want to add a Student
to the Classroom
, I might tell Student
to add itself to the classroom (classrooms can't find students, can they?). So, I will say student.addToClassroom(new Classroom(), this);
this
refers to the current Object
's reference.
Read this for more understanding.
To give an example from the link:
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Here, to differentiate from the x
of the Point
and x
of the argument, you need to tell the compiler the difference. You achieve that using this
. Meaning, when I write, this.x
it means, the particular x
belongs to the current Object
, which in the case is Point
.
Taking example from the code that you have provided:
AlertDialog.Builder(this)
AlertDialog.Builder() takes in a Context
as a parameter in its constructor. But here, you don't do Context someContext = new Context();
and pass that as the parameter, because you simply need to pass your current Activity
's Context
. So you simply use this
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With