Answer to Question #310674 in Java | JSP | JSF for Rush C

Question #310674

How do you call an instance variable in the main program?


1
Expert's answer
2022-03-13T09:35:37-0400
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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS