Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

"this" in java..  RSS feed

 
Praveena Gupta
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello guys,
i have a very beginer's question abt "this"

lets say-
public class PizzaStore //1 st line
{
SimplePizza sp;
public PizzaStore(SimplePizza sp)
{
this .sp=sp; // explaination on this line
}

can someone plz explain me the commented line,what does "this" do???
how to read this line,while explaining ..
like if for the 1 st line i wud say "there is a class named PizzaStore"
int he same way , how to read "this.sp=sp"

thanks,
Praveena
 
Manhar Puri
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will read it as there is a class variable sp of type SimplePizza which is being assigned a local variable sp of type SimplePizza.


In the end both these variables refer to the same object of type SimplePizza

Hope this explanation helps.

-Manhar.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Praveena Gupta:
hello guys,
i have a very beginer's question abt "this"

lets say-
public class PizzaStore //1 st line
{
SimplePizza sp;
public PizzaStore(SimplePizza sp)
{
this .sp=sp; // explaination on this line
}

can someone plz explain me the commented line,what does "this" do???
how to read this line,while explaining ..
like if for the 1 st line i wud say "there is a class named PizzaStore"
int he same way , how to read "this.sp=sp"

thanks,
Praveena


this is a way for an instance of a class to refer to itself.

In a method when you have a local variable with the same name as an instance variable, then that local variable is said to shadow the instance variable.

That means inside the constructor, any reference to sp by its simple name will refer to the parameter sp and not the instance variable sp.

To get around this shadowing, you can use this.

That line is like saying

Assign to this object's copy of sp the value of the parameter sp.
 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. public class PizzaStore //1 st line
2. {
3. SimplePizza sp;
4. public PizzaStore(SimplePizza sp) {
5. this .sp=sp; // explaination on this line
6. } }



In line 5, "this.sp" refers the "sp" in line 3 and "sp" refers to the "sp"(local variable passed as param to PizzaStore constructor) in line 4

what the line 5 actually does is, it stores the parameter passed in line 4 to the current instance of PizzaStore's instance variable sp, which is declared in line 3.

For clarity on this go to the following link, read, read and read then apply your mind and understand the concept.


What is an Object?
[ June 27, 2006: Message edited by: S Thanigaivel ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!