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.
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Oct 18, 2012
Please expain how below code is working I totally can't understand:
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?