this object?this.this for an intro-level student, w/r/t Java specifically?
this is dependent on that. (Har har.)self, the idea being "send a message to this instance of myself telling myself to do something". You're talking to yourself, basically. For languages that use this instead of self, you can think of it as sending a message or communicating with this specific object/instance of the class.this become fully graspable.this.int a = 45; this.a = a;) and secondarily to allow one really neat little trick (obiWan.help(this);). You might expound on those points excitedly. By the time she encounters anything else for which it might be used, she'll already grok Java.this is a reference, not an object or pointer. You don't name an object in Java, but always a reference. The difference is subtle, I suppose, but it's the difference between allocating space for a member object in the enclosing object's memory space or merely allocating the 48 bytes necessary to remember where in the heap the JVM stuck the object.Circle with member variable radius and method getCircumference() the following code:public double getCircumference() {
return 2.0 * Math.PI * this.radius;
}public double getCircumference() {
return 2.0 * Math.PI * my.radius;
}radius instead of this.radius, but you get the idea.
private String something; // this is the field
public void setSomething(String something // this is the parameter
) {
this.something = something; // they both have the same name, which is allowed
}
You can't use "this" to refer to static things, though. It's only used for instances of classes.this then simply points to that one specific object (try using the words "self awareness" or "self identity." I find that students respond well to anthropomorphism).
posted by demiurge at 2:00 PM on February 12, 2008