How do you call an instance variable in the main program?
public class Item {
public String name;
private double price;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public static void main(String[] args) {
Item item = new Item();
//if variable is public
System.out.println(item.name);
item.name = "Tom";
//if variable is private
System.out.println(item.getPrice());
item.setPrice(45.3);
}
Comments
Leave a comment