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 This keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "This keyword" Watch "This keyword" New topic
Author

This keyword

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
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).


Thanks,
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
To understand this keyword, you can take a look at:this keyword tutorial
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
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

Joined: Jan 01, 2007
Posts: 333

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

Joined: Aug 16, 2007
Posts: 1374
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

Joined: Apr 30, 2008
Posts: 41
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

Joined: May 03, 2004
Posts: 334
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.
 
wood burning stoves
 
subject: This keyword