2 Replies - 1137 Views - Last Post: 07 May 2007 - 08:32 PM Rate Topic: -----

#1 rkelly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-May 07

This Keyword

Posted 07 May 2007 - 04:16 PM

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;
Is This A Good Question/Topic? 0
  • +

Replies To: This Keyword

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: This Keyword

Posted 07 May 2007 - 05:38 PM

The this keyword is generally used to indicate reference to the current object:


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.
Was This Post Helpful? 0
  • +
  • -

#3 keems21   User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

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:
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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1