• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use Of "this" keyword

 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1819
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1819
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


..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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manish & Pawel for such a brilliant clarification
 
pardeepkumar devgon
Greenhorn
Posts: 8
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic