Results 1 to 4 of 4
Thread: how to use the "this"?
- 09-17-2008, 07:56 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
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:
Java 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 } }
- 09-17-2008, 11:41 PM #3
In general, I think of the this keyword as referring to the enclosing class. You can usually write
to find out where you are. Inner classes will have a "$" in their class name.Java Code:System.out.println(this.getClass().getName());
One place where this doesn't work well is inside the main method.
A couple more links:Java 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("---------------"); } } }
Using the this Keyword
this : Java Glossary
- 09-18-2008, 12:06 AM #4
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.
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
Hwlp with "Open", "Save", "Save as..."
By trill in forum New To JavaReplies: 3Last Post: 11-02-2010, 09:26 AM -
<core:forEach var="" begin="+<%=j%>+">???
By freddieMaize in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-27-2008, 01:20 AM -
[SOLVED] Exception in thread "main" java.util.NoSuchElementException
By thevoice in forum New To JavaReplies: 5Last Post: 05-14-2008, 01:43 PM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks