Hi Kat,
I remember how much time it took me to understand the concept of variable
class and when to use "this"... but its quite fundamental that you do so,
here it is:
A class has methods (functions).
Inside the methods, you can define variables that can only be seen inside
those methods. Other methods with variables with the same name will not be
conflicting because they cannot see eachother.
But there is a way of having variables that see eachother and that are
"above" the methods. Those are the class variables. They are in the scope of
the whole class (and probably to extended classes if you set them public)
and they are prefixed with the word "this." .
so:
*public* *void* setFruitName(String temp)
{
*this*.fruitName = temp;-------------> heres a class variable that can be
called from within any method and will be refering to the same one. In this
case you are setting it to have the value of temp
}
*public* String getFruitName()
{
*return* fruitName;---------> this one is a simple method variable and
exists only inside this method. You probably want to "return this.fruitName"
instead. I wonder if this one isn't coughing an error of an undeclared
variable....
}
Hope that makes sense!
Rodrigo.
2009/9/29 kat Owens <[email protected]>
> Hi all,
>
> I am trying to understand when to use the "this" keyword.
>
> The following is my code : inside the setFruitName whether I use
>
> this.fruitName = temp;
>
> or fruitName = temp;
>
> both seem to work. why is that ?
>
> Thank You,
>
> Kat
>
>
>
> * *
>
> *package*
> foodpackage.fruitpackage;
>
> *
>
> *
>
> *
>
> public
> * *class* Food{
>
> *private* String fruitName;
>
> *private* *double* fruitPrice;
>
> *private* *double* *memberPrice*;
>
> *private* *double* salePrice;
>
> *private* *boolean* *isMember*;
>
> *private* *int* *FriutsBought*;
>
> *public* *void* setFruitName(String temp)
>
> {
>
> *this*.fruitName = temp;
>
> }
>
> *public* String getFruitName()
>
> {
>
> *return* fruitName;
>
> }
>
> *public* *void* setFruitPrice(*double* fruitPrice)
>
> {
>
> *this*.fruitPrice = fruitPrice;
>
> }
>
> *public* *double* getFruitPrice()
>
> {
>
> *return* fruitPrice;
>
> }
>
> *public* *void* setSalePrice( *double* fruitPrice)
>
> {
>
> *if* (*this*.isMember = *true*)
>
> *this*.salePrice = fruitPrice/2;
>
> *else*
>
> *this*.salePrice = fruitPrice;
>
> }
>
> *public* *double* getSalePrice()
>
> {
>
> *return* salePrice;
>
> }
>
> *public* *void* setIsMember (*boolean* isMember)
>
> {
>
> *this*.isMember = *true*;
>
> }
>
> *public* *static* *void* main (String[] args){
>
> Food apples =
> *new* Food();
>
> apples.setFruitName(
> "APPLES");
>
> apples.setFruitPrice(0.50);
>
> apples.setSalePrice(apples.getFruitPrice());
>
> System.
> *out*.println(" The fruit name is " + apples.getFruitName());
>
> System.
> *out*.println(" The fruit name is " + apples.getFruitPrice());
>
> apples.setIsMember(
> *true*);
>
> System.
> *out*.println(" The fruit name is " + apples.getSalePrice());
>
> apples.setIsMember(
> *false*);
>
> System.
> *out*.println(" The fruit name is " + apples.getSalePrice());
>
> Food oranges =
> *new* Food();
>
> oranges.setFruitName(
> "ORANGES");
>
> System.
> *out*.println(" The fruit name is " + oranges.getFruitName());
>
> }
>
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---