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
-~----------~----~----~----~------~----~------~--~---

Reply via email to