aspose 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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 170
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
Pawel Pawlowicz
Ranch Hand

Joined: Apr 18, 2013
Posts: 602
    
  11

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:
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 170
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 ?
Pawel Pawlowicz
Ranch Hand

Joined: Apr 18, 2013
Posts: 602
    
  11

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: 170
Thanks Manish & Pawel for such a brilliant clarification
pardeepkumar devgon
Greenhorn

Joined: Aug 23, 2013
Posts: 4

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
java-latte.blogspot.com
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

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?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use Of "this" keyword
 
Similar Threads
Struts2.0 interceptor Problem
interceptor not working in struts 2
NullPointerException @ validation interceptor
struts2 problem
interceptor problem