Results 1 to 8 of 8
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default Head First Java book-this() question

    Hi, I have been having the hardest time understanding exactly what the "this" statement does. Supposedly it is a reference to the "current object". But I don't know what that means. Please if anyone can explain. Thank you. Derek

    Here is the code example.

    Java Code:
    public class SimpleGui1B implements ActionListener {
                   JButton button;
    
                   public static void main (String[] args) {
                         SimpleGui1B gui = new SimpleGui1B();
                         gui.go();
                   }
    
               public void go() {
                        JFrame frame = new JFrame();
                        button = new JButton("click me");
    
                        button.addActionListenter([B][COLOR="Red"]this[/COLOR][/B]);
    
    
                        ////more gui relevant code goes here
    
    
                 }
    
                  public void actionPerformed(ActionEvent event) {
                             button.setText("I've been clicked");
                  }
    
         }
    Last edited by silverglade; 05-06-2011 at 11:19 PM.

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

    Default

    You have no "this() method" but rather the this keyword (no method involved). It refers to the current instance of the class's object. Say your program has 3 SimpleGui1B objects. The "this" in the go method above will refer to whichever object is calling go. So if the first object has its go method called, this will refer to the first object. If the third object is calling its go method, this will refer to the third object, and so forth.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The this has many different uses. But the basic idea is it refers the the class you are currently working in. In this example you are using it to add action listeners. Normally, you would add an action listener like this
    Java Code:
    button.addActionListener(new SomeClassName())
    In this example, the class which has the action performed method is the class you are working in. You give it the reference this, which tells it to fire an action event to the classes action performed method. With this method you can have multiple buttons all listening to "this".

    There are some other uses of this. For example, in constructors and setters it's very common to have the passed in name have the same name as the classes instance variable. You differentiate between the instance variable and the passed in value via "this"

    Java Code:
    public class X{
      private int x;
      public X(int x){
        this.x = x;
      }
      public void setX(int x){
        this.x = x;
      }
    }
    This example shows this.

    The "this" keyword can also be used in constructors to use a different already created constructor. For example you can build your way to having multiple constructors like this
    Java Code:
    public class Y{
      private int x;
      private String z;
      private double x;
      public Y(int y, String z, double x){
        this.y = y;
        this.z = z;
        this.x = x;
      }
      public Y(int x, String z){
        this(x, z, 0.0);
      }
      public Y(int x){
        this(x, null, 0.0);
      }
    }
    I hope these explanations and examples help you out. Check this out too: Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  4. #4
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    LOL! Fubarable and Sunde887, that was an awesome explanation, clear and full. You just made me understand something that I never got for like a few years of learning to program off and on. First it was C++ which I stopped, and now once again, I saw this monster in Java. LOL! Thank you!!! Derek
    Last edited by silverglade; 05-06-2011 at 11:34 PM.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The first time I learned this I never was able to really understand it. It gets easier with time, and afaik the c++ version is the same as java.

  6. #6
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Thanks. Yes it was the multiple uses of this that confused me, but now I pretty much have it now, I will have to use it a few times and check it with the forum feedback to tell if I have really got it down. Right now I am just trying to finish the "Head First Java" book for the first time. I am going to have to reread it probably a few more times and each time will be a little faster, until finally I am just flipping through pages. And then also I have to type out the code for it to really sink in. Thank you both again. And thanks Sunde887 for your ongoing help. I REALLY appreciate it. DerekDD):):):)

  7. #7
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @Fubarable
    The "this" in the go method above will refer to whichever object is calling go.
    I think this line is wrong (the rest of the explanation is correct and you definitly mean it right). I think it should be:
    "The "this" in the go method above will refer to whichever object whose go() is called."
    (go() is public and can be called from another class. That's not the one you mean I guess.)

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

    Default

    Quote Originally Posted by Jodokus View Post
    @Fubarable

    I think this line is wrong (the rest of the explanation is correct and you definitly mean it right). I think it should be:
    "The "this" in the go method above will refer to whichever object whose go() is called."
    (go() is public and can be called from another class. That's not the one you mean I guess.)
    You are correct ,and thank you. I'd give you rep points, but I've already done that recently on you and have to spread around rep points to others before I can give you more (or so the forum software is telling me).

Similar Threads

  1. Head First Java book-Constructor question
    By silverglade in forum New To Java
    Replies: 28
    Last Post: 05-05-2011, 04:29 PM
  2. Head First Java Book question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 05-02-2011, 10:23 PM
  3. Head First Java book-Abstract methods question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 01:45 AM
  4. Is the Head First Java book wrong about Strings and GC?
    By raindog308 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 11:05 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
  •