0

I have trying to create an object based on the type using switch statement. However I am getting this error on the method in main method:

Cannot make a static reference to the non-static method CreateCoffee(CoffeeFactory.Type) from the type CoffeeFactory

public Coffee CreateCoffee(Type t ) {
    ingred = null;

    switch (t) {
     case LONG_BLACK:  
         ingred.add(Ingredient.ESPRESSO);
         return new Coffee(ingred, t);
     case FLAT_WHITE: 
         ingred.add(Ingredient.MILK);
         return new Coffee(ingred, t);
     case MOCHA: 
         ingred.add(Ingredient.CHOCOLATE);
         return new Coffee(ingred, t);

    default: return null;
    }


}

public static void main(String[] args) {
    CreateCoffee(Type.MOCHA);
}

what am i doing wrong, is it the return new Coffee() statement in each switch case?

Shaz
  • 1,443
  • 1
  • 27
  • 67

2 Answers2

1

You need to make the CreateCoffee method static as it is referenced directly from the main method which is static.

Mohit Mutha
  • 2,921
  • 14
  • 25
1

static enables you to call a method without creating any object.so if you're calling a static method in this case main then you can call methods with the help of objects or you can call other static methods.