• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The this keyword

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please expain how below code is working I totally can't understand:

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic