aspose file tools*
The moose likes Beginning Java and the fly likes The this keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The this keyword " Watch "The this keyword " New topic
Author

The this keyword

Rasul Patrick
Greenhorn

Joined: Oct 18, 2012
Posts: 20


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

Joined: Sep 13, 2007
Posts: 2601
    
    9

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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
Rasul Patrick
Greenhorn

Joined: Oct 18, 2012
Posts: 20
Please expain how below code is working I totally can't understand:

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 18, 2012
Posts: 20
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The this keyword