0

For some reason this works everywhere else except for coderpad. In every ide I had it works fine when I run it. This seems to make no sense to me. Is there an issue with the static setup or no?

class Main {
  
  public static class Restaurant {
  
    private final String name;
    private final String cuisine;
    private final double price;
    
    public Restaurant(String name,
                      String cuisine,
                      double price) {
      
      this.name = name;
      this.cuisine = cuisine;
      this.price = price;
    }
    
    public String getName() {
      return name;
    }
    
    public String getCuisine() {
      return cuisine;
    }
    
    public double getPrice() {
      return price;
    }
  }
  
  public static void main(String[] args) {
    
    
    int numRestaurants = 6;
    Restaurant[] myRestaurants = new Restaurant[numRestaurants];
    
    myRestaurants[0] = new Restaurant("Satisfactory Pita", "Mediterranean", 10);
    myRestaurants[1] = new Restaurant("Three Guys", "Burgers & Fries", 20);
    myRestaurants[2] = new Restaurant("China Panda", "Chinese", 30);
    myRestaurants[3] = new Restaurant("Puerto Sagua", "Cuban", 10);
    myRestaurants[4] = new Restaurant("Diya", "Indian", 40);
    myRestaurants[5] = new Restaurant("Shiraz Kabob Cafe", "Persian", 15);
    
    for(Restaurant myRestaurant : myRestaurants) {
      System.out.println(myRestaurant.name);
    }
  } 
}
  • Did you try declaring the class as `public`? – Arvind Kumar Avinash Oct 17 '20 at 19:07
  • @ArvindKumarAvinash Yes, and then I get "Solution.java:**: error: non-static variable this cannot be referenced from a static context myRestaurants[*] = new Restaurant(****);" on the new new restaurant parts –  Oct 17 '20 at 19:11

1 Answers1

0

According Languages support for run Java.

You should define a public class named Solution with a public static void main.

Rename the class to Solution, like:

class Solution{
  
  public static class Restaurant {
  
    private final String name;
    private final String cuisine;
    private final double price;
    
    public Restaurant(String name,
                      String cuisine,
                      double price) {
      
      this.name = name;
      this.cuisine = cuisine;
      this.price = price;
    }
    
    public String getName() {
      return name;
    }
    
    public String getCuisine() {
      return cuisine;
    }
    
    public double getPrice() {
      return price;
    }
  }
  
  public static void main(String[] args) {
    
    
    int numRestaurants = 6;
    Restaurant[] myRestaurants = new Restaurant[numRestaurants];
    
    myRestaurants[0] = new Restaurant("Satisfactory Pita", "Mediterranean", 10);
    myRestaurants[1] = new Restaurant("Three Guys", "Burgers & Fries", 20);
    myRestaurants[2] = new Restaurant("China Panda", "Chinese", 30);
    myRestaurants[3] = new Restaurant("Puerto Sagua", "Cuban", 10);
    myRestaurants[4] = new Restaurant("Diya", "Indian", 40);
    myRestaurants[5] = new Restaurant("Shiraz Kabob Cafe", "Persian", 15);
    
    for(Restaurant myRestaurant : myRestaurants) {
      System.out.println(myRestaurant.name);
    }
  } 
}
Eugen
  • 877
  • 6
  • 16