Hello,

There is a lots of reference material online

 

This might help:

 

You can refer to variables and methods in other classes along with
variables and methods in your own classes, the variable you're referring
to can become confusing in some circumstances. One way to make things a
little more clear is with the this statement. The this statement is a
way to refer in a program to the program's own object.

Look more on this link:

 

http://www.informit.com/library/content.aspx?b=STY_Java2_24hours&seqNum=
130

 

Regards,

Moria.

 

________________________________

From: [email protected]
[mailto:[email protected]] On Behalf Of kat
Owens
Sent: Tuesday, September 29, 2009 11:49 AM
To: [email protected]
Subject: [java programming] Lab - 1003 - this keyword

 

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