• Post Reply Bookmark Topic Watch Topic
  • New Topic

this keyword  RSS feed

 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is meant by this.object in following program, as this itself is a object:




thanks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be careful with your thread titles. You have now produced three threads with the same name, one of which has been closed.

You don't have this.object anywhere; I shall presume you mean this.obj.
If you write this.identifier it means that whatever called by that identifier which belongs to the object. The local variable of that name doesn't belong to the object, nor does the method parameter. The field does however. So by writing this.obj you are making it clear that you mean the field called obj. Otherwise the compiler would assume you mean the parameter (or local variable) by that name.

Plain simple obj = local variable or parameter.
Qualified this.obj = field.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am little bit unclear. please could you explain once again.
thanks
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well take this example:



The output of this program is:
Parameter variable
Instance variable

There are two possible variables called "stringVal" in the method. One is the method parameter, the other is the class variable.
You need to be able to distinguish between them - hence use of the keyword 'this'
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In 14 you're passing an instance of A4 which is an object to the constructor of B which takes an object as its parameter. That;s what the this does there. There's a couple things I've never seen before, but it works fine. The obj is just a variable name, not a keyword. Could be any legal variable name.


 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, whats the importance of this.obj in line 4. what does it mean?

thanks
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what's happening in your program!

On line 19 you create a new A4 object. When the constructor for this class "A4()" is called you create an object of type "B" is created. Notice that the constructor in class B takes an object of type A4 as an argument. So when you create an object of type B on line 15 you're passing the object that you just created (the reference for this object is "a" on line 19) as a parameter to the constructor (of B). Notice that an object of type B has an instance variable which is a reference to an object of type A4. You use the A4 object you passed in line 15 to initialize the instance reference of the B object!

Hope I helped!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!