This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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).
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 ]
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.