-
how to use the "this"?
i'm spanking new to Java. But i've done alot of PHP web programming so i'm not quite in the dark with languages.
I've been looking through the web on how to use "this", but i just can't find it anywhere. I'm probably not typing in the right keyword or something.
can any direct me, or show me how to use "this"?
-
It's sort of a broad question you are asking. Within any class's code, "this" refers to the actual object of this class. For instance if your class subclasses a JFrame you could call this.pack() to tell the object to pack itself. The most common use that I've seen with it is if you have a method or a constructor whose parameter names are the same as the object's instance variables. Then "this" is used to help the method or constructor distinguish which variables are the object's instance variables (those prefaced with "this") and which are parameter variables (those without). For instance:
Code:
class Fubar
{
private int value;
public Fubar(int value) // constructor with parameter same name as instance variable
{
this.value = value; // the object's value variable is set equal to the value parameter
}
}
-
In general, I think of the this keyword as referring to the enclosing class. You can usually write
Code:
System.out.println(this.getClass().getName());
to find out where you are. Inner classes will have a "$" in their class name.
One place where this doesn't work well is inside the main method.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ThisTest extends JPanel {
ThisTest selfReference = this;
{
printName(this, "initializer block");
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (getWidth() - 100)/2;
int y = (getHeight() - 100)/2;
g.setColor(Color.blue);
g.drawOval(x, y, 100, 100);
printName(this, "this in paintComponent");
}
private JPanel getContent() {
this.addMouseListener(ml);
HelloWorld helloWorldInstance = new HelloWorld();
System.out.println("-------getContent-------");
printName(this, "this");
printName(ThisTest.this, "ThisTest.this");
printName(helloWorldInstance, "helloWorldInstance");
System.out.println("--------------");
return this;
}
private void printName(Object o, String source) {
System.out.println(source + ": " +
o.getClass().getName());
}
public static void main(String[] args) {
ThisTest test = new ThisTest();
test.printName(test, "test in main");
test.printName(test.selfReference, "selfReference in main");
//System.out.println(this); // compile error
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test.getContent());
f.setSize(300,200);
f.setLocation(200,200);
f.setVisible(true);
}
private MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("-------ma-------");
printName(this, "this");
printName(selfReference, "selfReference");
System.out.println("superclass: " +
this.getClass().getSuperclass().getName());
System.out.println("--------------");
}
};
private static class HelloWorld {
public HelloWorld() {
System.out.println("-------HelloWorld-------");
System.out.println("this: " + this.getClass().getName());
System.out.println("declaring class: " +
this.getClass().getDeclaringClass().getName());
System.out.println("---------------");
}
}
}
A couple more links:
Using the this Keyword
this : Java Glossary
-
like this ;)
I think of it as if one were going to do 20 new varables, then the keyword refers to "this one, dummy, which ever one we are in." The alternative is static, which is for something that is always the same in every instance, there is no keyword that means the opposite of static.