Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion with 'this' keyword  RSS feed

 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that 'this' inside a method refers to the object that called(invoked) the method. But while discussing it another thread I was told that its not the correct way to put it and "Rather, this refers to the current object on which the method was invoked. It's entirely possible that a different object could have called/invoked the method. ". My confusion is that how can a object call/invoke the method of an entirely different object?
For e.g. even if the object of class A wants to call the method of class B, then doesn't it need to first have an object of class B to call its method(and therefore the 'this' in the called method would refer to B because B is the object that called(invoked) the method)? I think I am overlooking some concept...



I created a separate thread as I did not want to hijack the OP's. Thanks.
 
Piet Souris
Rancher
Posts: 1979
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Vaibhav,

I think what the other peopkle say is correct. Unfortunately, the wording is such that it is difficult to understand for someone who is not a native english speaking person like meTherefore I prefer to stick to some simple examples. I based the following code on your code:


See for yourself to what objects all those 'this'ses belong.

The cases where I had to think about the object at hand were often when I had a class extending a JFrame. In that frame, there was a JPanel defined, that used an ActionListener. Now, if you use 'this' within this ActionListener, is this 'this' referring to theframe, to the panel or to the actionlistener? When in doubt, let your IDE come to help. Type 'this.' and then in the pop-up window you can identify the 'this' by the methods it shows.
 
Stephan van Hulst
Saloon Keeper
Posts: 7797
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav Sagar wrote:For e.g. even if the object of class A wants to call the method of class B, then doesn't it need to first have an object of class B to call its method(and therefore the 'this' in the called method would refer to B because B is the object that called(invoked) the method)?

You say object "a" wants to call object "b"'s method. Then you say "b" is what called the method. This is a contradiction, and where the confusion lies. If "a" calls a method, "a" calls a method. If "b" calls a method, "b" calls a method.

In either case, 'this' always refers to the object the method was called on.
 
sivakumar reddy
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is one of the java keywords and it helps in referring to the current object. When we use the dot(.) operator on this keyword, then we can access the member variables of the current object. this keyword can also be used to call one constructor from another constructors of the same class.

The parameter names passed to the constructors can have the same name as the member variables of that class.
For e.g., the constructor with parameters nameParam, marksParam and sectionParam

can be rewritten as

If we have the parameter name same as the member variable, then the parameter hides the member variable. That is even if we assign value to the parameter, then the member variable is not affected. We can use the this keyword to distinguish the member variable name from the parameter name. When this.name is called it is the member variable, and when only name is called it is the parameter. Similarly, this.marks and this.section are member variables, where as marks and section are parameters.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav Sagar wrote:I thought that 'this' inside a method refers to the object that called(invoked) the method. But while discussing it another thread I was told that its not the correct way to put it and "Rather, this refers to the current object on which the method was invoked.

Like the others, I have a slight preference for the term "current object", but there are three basic contexts where you can use the 'this' keyword, and each one is slightly different:

1. (The one you're probably most familiar with) inside an instance method:
this refers to the object the method was called on.

2. Inside a constructor:
this refers to the object being created.

3. (Which you probably haven't run into yet) inside an inner class constructor or instance method:
Outer.this refers to the object that contains the current one (where 'Outer' is the name of the containing class).

Don't worry about #3 too much. It doesn't crop up very often, and you'll (hopefully) be shown how it works if you ever need to use it.

It should probably be added that in case 1, you don't generally have to use the this keyword - especially if it's followed by a '.' - since it will be assumed; but it can often make things clearer if you do.
In case 2, my advice would be to always use it, especially if there may be a confusion between a parameter name and a variable name. For example:
HIH

Winston
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav, you seem to just be confused by the meanings of "called by X" vs "called on X"

In your example, the printIt() method is being called on the object b. That is, b is the object whose printIt() method was called. In proper English you would say that "b is the object on which the printIt() method is called." If you're not a strict grammarian, you could also say, "b is the object that the printIt() method is called on." I was simply using the more grammatically correct phrasing in the other thread you mentioned.

Still referring to your example, class A is making the call. That is, the expression b.printIt() is in Class A, specifically in its main() method. That makes Class A the "caller" and therefore, the proper English is "The printIt() method is being called on object b by Class A's main() method."

In summary, when you say "b.printIt() is called by X", X refers to whatever contains the expression b.printIt() and that could be any object.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SR, welcome to the Ranch

I have taken the liberty of removing a link from your post because you said in your other post that link was no good. Please don't post that link again if it is no good.
 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for clearing this up. I was confused with the grammar and the wordings and I thought there is some concept that I am not aware of regarding 'this' . With Java one needs to be sure cause there are so many little gotchas here and there (IMHO).
 
Stephan van Hulst
Saloon Keeper
Posts: 7797
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I think this is also the reason why people on these boards try to be very precise with their wording, and nitpick whenever somebody uses the wrong words. "You know what I mean" carries little weight. We don't do it to annoy people.
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:...people on these boards try to be very precise with their wording, and nitpick whenever somebody uses the wrong words. "You know what I mean" carries little weight. We don't do it to annoy people.

Speak for yourself

Stephan is right, as programmers we need to be very precise with our wording and make sure that we are saying what we mean. Not doing so is what leads to bugs and too many hours of "discussions" and debugging.

You may have heard this story before:

One day, a programmer's wife calls him and says, "Hi honey, please stop by the store and buy a gallon of milk on your way home tonight. And if there are any eggs, buy a dozen."

The husband comes home with twelve gallons of milk and the wife asks, "Why did you buy so much milk?!"

The programmer shrugs and says, "There were eggs!" ¯\_(ツ)_/¯
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!