whats the meaning of this in java. I can't seem to find a clear explanation.
For example, what does getActionListener(this) mean in comparison to naming the type of event.
or if you have an object. fish(int size); size = this.size;
This Keyword
Page 1 of 12 Replies - 745 Views - Last Post: 07 May 2007 - 08:32 PM
Replies To: This Keyword
#2
Re: This Keyword
Posted 07 May 2007 - 05:38 PM
The this keyword is generally used to indicate reference to the current object:
http://java.sun.com/...OO/thiskey.html
In the example you posted, the variable named size is being set to the current value stored in the size member of the current object.
http://java.sun.com/...OO/thiskey.html
In the example you posted, the variable named size is being set to the current value stored in the size member of the current object.
#3
Re: This Keyword
Posted 07 May 2007 - 08:32 PM
Yeah, this concept tripped me up for a while too. The two examples that you give are very different, but I'll try and explain them both.
In your first example, you use: getActionListener(this). This is used in the case that the class that you create implements ActionListener. So, you should find this somewhere where you have something that looks like this:
In this case, you need to consider MyClass as an object, and one of that object's properties is that it "implements ActionListener." If you wanted to access MyClass's ActionListener property, you need to use the "this" modifier.
This is very similar in the other case that you mention. In this instance, you would have something like this for your class file:
Notice that there are two int variables with the name "size." There needs to be a way to distinguish between the two of them. When you call: this.size, you are calling the int variable size that is a property of the class MyClassTwo. The second size is refrencing the arguement passed through the method signature. So in essance, you are taking the variable from the method and setting it to the variable that defines the class.
I hope that this helps a little. If not, I can try and explain it in another way. Good luck.
In your first example, you use: getActionListener(this). This is used in the case that the class that you create implements ActionListener. So, you should find this somewhere where you have something that looks like this:
public class MyClass implements ActionListener
In this case, you need to consider MyClass as an object, and one of that object's properties is that it "implements ActionListener." If you wanted to access MyClass's ActionListener property, you need to use the "this" modifier.
This is very similar in the other case that you mention. In this instance, you would have something like this for your class file:
public class MyClassTwo{
private int size;
public int fish(int size){
this.size = size;
}
}
Notice that there are two int variables with the name "size." There needs to be a way to distinguish between the two of them. When you call: this.size, you are calling the int variable size that is a property of the class MyClassTwo. The second size is refrencing the arguement passed through the method signature. So in essance, you are taking the variable from the method and setting it to the variable that defines the class.
I hope that this helps a little. If not, I can try and explain it in another way. Good luck.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|