I am unable to understand the use of "this" keyword in calling a method.
All i have used "this" keyword is accessing class variables & constructors.
What is the meaning of " this.getRequest() " in the following code .
I am using Spring & Struts2 . Code is as follows:
Pawel Pawlowicz wrote:A statement this.getRequest() means: call the method getRequest for this instance of HomePageAction.
In this case it is redundant. You might skip it and call:
Thanks Pawel . You mean getRequest method is called by the object of the HomePageAction ( which has been created implicitly by the Struts2 Framework ) . Am i right ?
That's right. createHome() method of HomePageAction class is public so any other class may call it.
Then the object on which createHome() was called calls getRequest() on itself. That's the meaning of this in that case.
A keyword this has a self-explanatory meaning. It means "a reference to this object". And "this object" means "an object that owns a code currently running".
1. It clarify that you are talking about a field, when there is another variable of the same name.
refer to current object as stated above
2. invoke other constructor of the current class in your parameterized constructor.
3. It can be used to return the instance of a class
4. this can also be used to refer to the outer object
Note: One point to remember is that this is associated with the instance of the class, so it will not work in static methods
When you're answering questions, please KeepItDown (←click). Lots of bolding coming across as shouting!!!, and I'm sure that wasn't your intention. I'd also be very sparing about using colour (although what you did was fine).
Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here