It's not a secret anymore!*
The moose likes Beginning Java and the fly likes understood the concept of this keyword but unable to understand a small program with respect to it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "understood the concept of this keyword but unable to understand a small program with respect to it" Watch "understood the concept of this keyword but unable to understand a small program with respect to it" New topic
Author

understood the concept of this keyword but unable to understand a small program with respect to it

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

data members having this is actually a reference to a current object..is very much clear but this code is damn confusing me can anyone simplify the explaination about the execution if the coding



http://www.lifesbizzare.blogspot.com || OCJP:81%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Please use the CODE button; your code is illegible without. I edited your post with the CODE button and found your indentation is wrong, so it is still difficult to read.

You will find lots of old threads about that keyword; I found no 1, No 2, No 3 and No 4.

In the instance you are using, the printer object calls the static "out" field of the System class, which calls its println() method and prints the object you happen to be in at the moment. Details of the println method and how it handles an object can be found in here and here in the API documentation.
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

Your code after modifications looks




here you are passing Data object in to the print() method and thats the reason you could access the getData() method in the printer class. Also as the printer object was created and then p.print() was called, this might confuse you as which object is passed .. but as the keyword this is called from inside the class Data, the object of type Data is passed.

 
GeeCON Prague 2014
 
subject: understood the concept of this keyword but unable to understand a small program with respect to it