This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is "this" ?  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am VERY confused about "this". I know how to use it in programs but dont really know what it is . Books (k&b) say "this" is a reference to the currently executing to object. What is meant by "currently executing" object ? As per my understanding only a method can be "currently executing" while objects just lie in memory. There could be so many objects in memory when i run my program ? how does "this" refer to one of these many objects ? What exactly is happening ?
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you're executing a method (a non-static one), it's always on an instance of an object. "this" just refers to that instance.

This is why "this" doesn't work in static contexts.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to expand on what Luigi said. Lets say you've got this class:
Then later:
On line 2 there, we're calling the doSomething() method on the obj instance of MyClass. That means that inside doSomething(), this refers to the same object as obj was referring to. The overall effect is the same as if we'd called System.out.println(obj).

Does that make sense to you?
 
Karl Lockhart
Greenhorn
Posts: 1
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way 'this' was explained to me to make me understand it, 'this' refers to the instance of the class it appears in. So weird analogy coming, if you are riding in a train... told you it was weird... and you are stopped at a station, though there are many stations, this station is the one you are currently in... gets even weirder think of yourself as the 'execution pointer' in my weird analogy... so the platform of this station that you are currently in would be this.platform :p hope that makes sense... just trying to be helpful right out the gate... If it were a static, i.e. one station or one shared platform, it is simply the platform, so no need for this... i.e. no matter where you were... the platform would always be the one platform...
 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Follow Karl. He's on the right track.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think something that makes this tricky to understand is that the code in a method can be thought of as belonging to the class -- we write it in the class definition, and the same method will be used regardless of what instance it is used on.

Let us consider a cat class:



I can instantiate any number of cats of different colors; getColor() returns the color of the particular cat instance on which I call the getColor method.

If some class decides that I want to make a particular instance my cat, it could call the "takeOwnership" method, which in turn calls the static method to add this cat to my collection. But it needs a way to refer to the cat I want to own; 'this' is the keyword that does that. It passes a reference to the instance of cat on which that method was called to that other method.



In this example fragment, I am taking ownership of the Siamese, but not the orange tabby. The takeOwnership method will pass a reference to lilCat to the CatCollection method that it calls.

rc
 
Stephan van Hulst
Saloon Keeper
Posts: 7802
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Byron wrote:Follow Karl. He's on the right track.

 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We shall have to get you to train us. And welcome to the Ranch
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Lockhart wrote:...'execution pointer'...


I am not very comfortable with analogies in general, but anyway what is this "execution pointer" ? Is it something in java ? I am a non-CS guy, so is this something i must know about ?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No that is nothing in Java. Java doesn't know about pointers only references. It's important to call them references and not pointers (although they are similar) because people from a c/c++ background will be very confused if you call them pointers. It's just an analogy.
 
Stephan van Hulst
Saloon Keeper
Posts: 7802
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can think of the execution pointer as the location of your code that is currently executing.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:You can think of the execution pointer as the location of your code that is currently executing.


hmm...from these replies and my memory of C, 8086 (from electronics) and java i guess this is what it is (if incomplete/wrong, please rectify) :

C:


java :

each move() is unique and is move legs,flap wings, flap fins respectively.

So "this" is the "execution pointer" which tells the computer how execute a program, it holds the instruction to be processed. So , there is only ONE "this". Where is it located ? Is it something like a special register in the processor.

PS : Am i making this look childish and long-winded ? I sometimes get worried that i am not keeping things simple enough and there is a much simpler way of doing what i am trying to do


 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try writing some code and looking at the bytecode with the javap -c command?Then try again with null replacing this.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why don't you try writing some code and looking at the bytecode with the javap -c command?Then try again with null replacing this.


I am unable to understand what is happening.

with NULL, i got this compiler error :


With THIS and javap -c Foo , i got this :
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try casting the null. Try (Foo)null instead of plain null. See whether you can see any difference in the bytecode. Similarly try new Foo() instead of this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!