File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Use Of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use Of "this" keyword" Watch "Use Of "this" keyword" New topic
Author

Use Of "this" keyword

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
Hello Everybody
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:

Mycustom.xml file
applicationContext.xml

HomePageAction.java file
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1012
    
  16

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:


Formely Pawel Pawlowicz
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
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 ?
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1012
    
  16

Tarun Oohri wrote:
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".
manish ghildiyal
Ranch Hand

Joined: Jan 12, 2013
Posts: 136


..what it means is that the instance on which method createHome() is called, on very same instance method getRequest()
is called.

Manish
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
Thanks Manish & Pawel for such a brilliant clarification
pardeepkumar devgon
Greenhorn

Joined: Aug 23, 2013
Posts: 8

Just to add

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

Pardeep.Kumar
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

pardeepkumar devgon wrote:Just to add...

Hi pardeepkumar, and welcome to JavaRanch.

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).

Thanks

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use Of "this" keyword