Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

this keyword

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



OUTPUT :
scrap@15db9742  scrap@15db9742
scrap@15db9742  scrap@6d06d69c


When method m() is called on line 1, the 'this' inside m() refers to the object created on line 1.And it is because ' this ' refers to the current object on which the method is called. But  I had expected that on line 2 , when another new scrap object is used to call n() method which is passed a 'this' , the ' this ' being passed on this line 2 would refer to the object created on line 2 !

But on observing the output printed from the n() method,  I see that throughout m(), any ' this ' is refererring to the line 1 object ! I don't get it why ! On line 2, the current object calling n(this) is the line 2 object right ? why is it considered only inside n()  ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'this' in line 2 still refers to the object on which the method m() was called (in line 1).

You are explicitly passing that object to the n() method there.

In the n() method, the variable 'o' refers to the object that was passed to it, which is the object on which m() was called - because in line 2, you are explicitly passing that object to the method n().

Of course, the 'this' in the method n() refers to the object on which the n() method was called, which is the object created in line 2, on which you call the n() method.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the 'this' in the method n() refers to the object on which the n() method was called, which is the object created in line 2, on which you call the n() method.



when ' this ' inside n() definition would refer to the line 2 object, then why not the ' this ' inside the n() call in new scrap().n(this); when its quite clear that n is being invoked on the new object
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be helpful if someone could provide their help.


since its been one day. not bumping my post, just a small request.
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:
when ' this ' inside n() definition would refer to the line 2 object, then why not the ' this ' inside the n() call in new scrap().n(this); when its quite clear that n is being invoked on the new object



I am not really understand the argument here, the this reference, from an non-static context (in your second example) refers to the caller. It doesn't matter what the object being called on is.... and later, from the context of the callee, it is *not* the this reference variable; it is one of the parameters.

Or, in other words, from the caller side, if you use the this variable to call the method (passing the this reference), then from the callee side, the this variable and the parameter will be the same. On the other hand, from the caller side, if you use a different instance to call the method (passing the this reference), then from the callee side, the this variable and the parameter will be different.

Henry
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my only doubt is this part :

the this reference, from an non-static context (in your second example) refers to the caller. It doesn't matter what the object being called on is.

So the ' this ' context always depends on the current method definition within which its used ?
 
Bartender
Posts: 4007
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If in doubt, always use some handy print-statements.
I've added some to your code, see if you understand the output better:
 
Henry Wong
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:
So the ' this ' context always depends on the current method definition within which its used ?



The this reference is *always* pointing to its own instance. So, for the caller instance method, the this reference is pointing to its own instance. For the callee method, the this reference is pointing to its own instance.  And obviously, in the first case, both this references are pointing to the same instance. And for the second case, the this references are not pointing to the same instance.

Henry
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry , Okay thanks!

Piet , it helped ! thanks !
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic