简体   繁体   中英

why does this code keeps on showing error

public class Store {

    //instance fields 
    String productType;
    int inventoryCount;
    double inventoryPrice; 

    //constructor method
    public Store(String product,int count,double price) {
        productType = product;
        inventoryCount = count;
        inventoryPrice = price;
    }

    //main method 
    public static void main(String[] args) {
        Store cookieShop = new Store("cookies",12,3.75);
        System.out.println("my cookie shop menu  " + cookieShop.product);
    }
}

reason why this error is constantly showing?

Store.java:16: error: cannot find symbol
    System.out.println("my cookie shop menu  " + cookieShop.product);
                                                           ^
  symbol:   variable product
  location: variable cookieShop of type Store
1 error

The field in the class is named productType , you are referencing it by its name in the constructor signature. So use:

    Store cookieShop = new Store("cookies", 12, 3.75);
    System.out.println("my cookie shop menu  " + cookieShop.productType);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM