• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do we put this in the addActionListener

 
Sergiu Dobozi
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this refers to the object named 'object' in main, why not simply write object instead of this?

 
Junilu Lacar
Bartender
Pie
Posts: 8771
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because "this" is not a reference to a specific object. Rather, it refers to whatever object happens to be the one whose method is being invoked. The "this" reference is a generic term, much like "you" often does not refer specifically to Sergiu Dobozi all the time in a sentence like "You should always wash your hands before eating."
 
Sergiu Dobozi
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Because "this" is not a reference to a specific object. Rather, it refers to whatever object happens to be the one whose method is being invoked. The "this" reference is a generic term, much like "you" often does not refer specifically to Sergiu Dobozi all the time in a sentence like "You should always wash your hands before eating."

So when the compiler reads "this" does it call the constructor for the current class the method is in?
 
Junilu Lacar
Bartender
Pie
Posts: 8771
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:So when the compiler reads "this" does it call the constructor for the current class the method is in?

No, "this" is a reference to the current object; "this(...)" will call a constructor of the current class with a matching set of parameters, if there is one.
 
Sergiu Dobozi
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Sergiu Dobozi wrote:So when the compiler reads "this" does it call the constructor for the current class the method is in?

No, "this" is a reference to the current object; "this(...)" will call a constructor of the current class with a matching set of parameters, if there is one.


So if we replace the keyword 'this' with any other actual object the code as we have it now will not compile?
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends whether the other object implements action listener or not. I personally believe you shou‍ld never make a display Component implement action listener. Any instance of a class which implements action listener can be used instead.
 
Fred Kleinschmidt
Bartender
Posts: 410
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not simply write object instead of this?

When you create an object of type SomeClass using  an instance of that class is created and assigned to the variable "object". But the instance does not know anything about the name you gave it in main. Consider:

Now in the SomeClass constructor, would you use "object", "another", or "third" ? No. The calls to create the three instances are very likely to be in a completely different file, so there would be no way of knowing the names of the variables for the three instances. That is why we use the term "this" in the SomeClass code, to specify whichever instance is currently being referred to.
 
Sergiu Dobozi
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:
why not simply write object instead of this?


Now in the SomeClass constructor, would you use "object", "another", or "third" ? No. The calls to create the three instances are very likely to be in a completely different file, so there would be no way of knowing the names of the variables for the three instances. That is why we use the term "this" in the SomeClass code, to specify whichever instance is currently being referred to.


So addActionListener really in fact is taking just a reference to an object of the class it is in as a parameter. And all of this is happening in a constructor of that same class, where you are not allowed to create objects of the class that addActionListener is in?
Do I understand correctly? 
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think you have understood it. There is no other way to refer to the current object than this. You are taking the object you are inside, which is being constructed by the new operator and the constructor.
It still doesn't make it good practice for action listeners, however.

It is possible to create objects of the same class inside a constructor. Look at this class. Execute its main method:-See, you can create an instance of the same class in the constructor. It might not work very well, but it can be done
 
Fred Kleinschmidt
Bartender
Posts: 410
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Campbell's example of creating another instance in the constructor creates infinite recursion and will eventually crash with a stack overflow.
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is why I said it might not work very well
 
Sergiu Dobozi
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That is why I said it might not work very well


This program also compiles.
And it works equally well.
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:. . . This program also compiles. . . .
Are you sure about that? We would need to see that whole class definition to know whether it compiles or not.
 
Sergiu Dobozi
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are you sure about that? We would need to see that whole class definition to know whether it compiles or not.

Here it is:
 
Henry Wong
author
Marshal
Pie
Posts: 22092
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, compiling or not, I am pretty sure this code doesn't work -- unless of course, you intended to get a stack overflow (or a out of memory) condition.

Henry
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:So addActionListener really in fact is taking just a reference to an object of the class it is in as a parameter.

No. It takes a reference to any object that implements the ActionListener interface. Which the current class in your example happens to do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic