Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This keyword

 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I,m new to java.Please let me know the meaning of this keyword,how to use it and when to use it.

the example program is below:
public class Pet implements Comparable {
int petId;
String petType;
public Pet(int argPetId, String argPetType) {
petId = argPetId;
this.petType = argPetType;......(1)
}

What can be the idea behind using this keyword at (1).
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand this keyword, you can take a look at:this keyword tutorial
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But would like to know why is it mention at (1) and not at (2)

public Pet(int argPetId, String argPetType) {
this.petType = argPetType;......(1)
petId = argPetId;...............(2)
}
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public Pet(int argPetId, String argPetType) {
this.petType = argPetType;......(1)
petId = argPetId;...............(2)
}


Its use at one is an attempt to remove ambiguaty, by saying that you are assigning argPetType to the field petType of this (the current object).
I dont think it is really need in that example.
In the example below, both field name and paremeter name are the same, so we need to tell Java that we wish to assign someValue to eh instance variable otherwise Java will just try to assign someValue to itself.



You might, in future, wish to put code tags around your code snippets, as this will perserve the codes formatting and make it easier to read.

[ May 29, 2008: Message edited by: Gavin Tranter ]
[ May 29, 2008: Message edited by: Gavin Tranter ]
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Phillipe,

Please use CODE tag to enclose your code.
But would like to know why is it mention at (1) and not at (2)


It's not mandatory. You can remove 'this' from (1) or add it to (2) for this particular case.

I suggest you to go through the tutorial suggested by Amitabh. You will have very clear idea about 'this'.
 
Karthikeyan Ramaswamy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

this keyword is current class's object.

this is a simple term to tell.

----------------------------------
[ May 29, 2008: Message edited by: Karthikeyan Ramaswamy ]
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gavin Tranter:


Its use at one is an attempt to remove ambiguaty, by saying that you are assigning argPetType to the field petType of this (the current object).
I dont think it is really need in that example.
In the example below, both field name and paremeter name are the same, so we need to tell Java that we wish to assign someValue to eh instance variable otherwise Java will just try to assign someValue to itself.



You might, in future, wish to put code tags around your code snippets, as this will perserve the codes formatting and make it easier to read.

[ May 29, 2008: Message edited by: Gavin Tranter ]

[ May 29, 2008: Message edited by: Gavin Tranter ]



Your explanation is correct. Usually the keyword this is used for referring the current instance. Apart from this, the keyword this can be used to assign values to instance variables where local variables hides the instance variables.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic