Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The this keyword

 
Rasul Patrick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why I am getting this output: Leaf@3e25a5 ? What does it mean ?
Also take a look at my comments please correct me if I'm wrong.
 
K. Tsang
Bartender
Posts: 3524
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your output of "Leaf@3e25a5" is a string representation of the instance Leaf. You see this because implicitly the println() method changes the Leaf object to a string by calling the Object class's toString(). By definition (according to the API), the stuff after the @ sign is the hashcode of that instance. Again this hashcode is calling the internally hashcode() method of the Object class.

Your question is not really about the "this" keyword but why the output has the "@xxxx" at the end.

In your code, it is not necessary to return "this" or the class itself for the increment() method because your variable "i" is an instance variable.

About the "this" keyword, it is used for referencing the current object's or instance's variable, methods etc.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the JVM supposed to know how to print out an object you create? It can't magically know that you want it to print. the JVM calls your object's toString() method, which you have not overridden, so you get the default one implemented in the Object class.
 
Rasul Patrick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please expain how below code is working I totally can't understand:

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rasul Patrick wrote:Please expain how below code is working I totally can't understand:


What in particular don't you understand?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:the println() method changes the Leaf object to a string by calling the Object class's toString().


No, it doesn't change the object at all. Calling toString() is no different than calling any other method. It just returns a value, and the caller uses that value.
 
Rasul Patrick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Rasul Patrick wrote:Please expain how below code is working I totally can't understand:


What in particular don't you understand?



Line no. 18

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rasul Patrick wrote:
Jeff Verdegan wrote:
Rasul Patrick wrote:Please expain how below code is working I totally can't understand:


What in particular don't you understand?



Line no. 18



new Person() -- says create a new Person object
. -- says we're going to dereference the pointer returned by the above, and "go into" the object, that is, access one of its member variables or methods.
eat(...) -- says invoke the eat() method on the Person object pointed to by the reference returned by new Person()
new Apple() -- says create a new Apple object. The fact that it's inside the eat(...) call means we're passing a reference to that newly created Apple() to the eat() method

It's equivalent to this:


Do you understand it when it's expressed that way?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic