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

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

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Campbell Ritchie
Sheriff
Pie
Posts: 50246
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic