![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
hey...
i was reading the text which our school require us to read...this book mentioned something about the "this" parameter, which is a hidden parameter passed to the methods or any other stuff that need the reference to the object......i was thinking that if i would be able to return this variable from the method i could reference some object created by this method....my question is how do i do it....i was trying many stuff...i have also checked the web for this ....."this" word is to common to be searched by the engines ..at least some of them...lol... help me if u can!!! thanx in advance!!!!! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
If you have a class with local parameters, they can be accessed with the 'this' method. 'this' is just an identification method pointing to the current object. For instance, when programming a class... the current object is that class. Basically, when programming classes(or objects) the 'this' keyword acts as a pointer to the inherited traits of the object when in use by the program (sigh i know that made no sense). Even more basically, its used when programming classes to say to call itself as if someone who used it in the program would under the variable they declared, in place of the variable they declared you can call internally the functions of your class with the 'this' keyword (if that made any sense).
I give up.
__________________
Last edited by tempest; Jan 26th, 2005 at 8:34 PM. |
|
|
|
|
|
#3 |
|
Programmer
|
I could be off-base here, never having programmed in Java - but this sounds like the "this" in C++....so....
You have a class you've called "DrinkingBuddy". This class has a few attributes (like name, age, colour, etc) and a few methods (like drink(), puke(), and passout())....so you instantiate an object of your DrinkingBuddy class called "Todd". You then call the drink() method using your Todd object (Todd.drink(rum) ) - in the drink() method code, if you wanted to refer to the todd object (say to specify an attribute) you would say "this->colour = green;" (the actual java code would probably be different, but you get the idea)....so the "this" refers to the Todd object. If you later instantiated another DrinkingBuddy object called "Jeff", and used the drink() method, the "this" would then refer to the "Jeff" object, as that was the one that called it. To answer your question - because the "this" refers only to the object that called the method the "this" actually appears in, you couldn't use it to pass that object around... I hope that makes sense for Java...(or for C++ for that matter) - if not, please correct me.
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~ read my blog @ My Lucky Rocketship Underpants
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
Looks like a great explanation to me...
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|