Results 1 to 8 of 8
- 05-06-2011, 10:16 PM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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 10:19 PM.
-
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.
- 05-06-2011, 10:26 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
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".Java Code:button.addActionListener(new SomeClassName())
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"
This example shows this.Java Code:public class X{ private int x; public X(int x){ this.x = x; } public void setX(int x){ this.x = x; } }
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
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)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); } }
- 05-06-2011, 10:31 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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 10:34 PM.
- 05-06-2011, 10:33 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 05-06-2011, 10:47 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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. Derek
D
D
):):):)
- 05-07-2011, 12:35 AM #7
@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 is calling go.
"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.)
-
Similar Threads
-
Head First Java book-Constructor question
By silverglade in forum New To JavaReplies: 28Last Post: 05-05-2011, 03:29 PM -
Head First Java Book question
By silverglade in forum New To JavaReplies: 4Last Post: 05-02-2011, 09:23 PM -
Head First Java book-Abstract methods question
By silverglade in forum New To JavaReplies: 4Last Post: 04-30-2011, 12:45 AM -
Is the Head First Java book wrong about Strings and GC?
By raindog308 in forum New To JavaReplies: 2Last Post: 02-07-2011, 10:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks