Results 1 to 4 of 4
  1. #1
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default 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"?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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
      }
    }

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    In general, I think of the this keyword as referring to the enclosing class. You can usually write
    Java 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.
    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("---------------");
            }
        }
    }
    A couple more links:
    Using the this Keyword

    this : Java Glossary

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default 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

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 02:20 AM
  3. Replies: 5
    Last Post: 05-14-2008, 02:43 PM
  4. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 04:42 AM
  5. Replies: 1
    Last Post: 07-25-2007, 11:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •