1

I have created an ArrayList and then I tried to put values in it. But after I have more than one values. Every Object in the List is replaced by the last one.

This is my declaration:

public static List<Dish> menu = new ArrayList<>();

public static void setMenuList(){
   menu.add(new Dish(1,19.99, "Mocha" , 5));
   System.out.println(menu);
   menu.add(new Dish(2,0.99, "Pok tar" , 5));
   System.out.println(menu);
   menu.add(new Dish(3,10.99, "Klingon Bloodwine" , 5));
   menu.add(new Dish(4,13.45, "Gagh" , 5));
   menu.add(new Dish(5,12.95, "Bahgol" , 5));
   menu.add(new Dish(6,08.00, "Racht" , 5));
   menu.add(new Dish(7,09.99, "Osol Twist" , 5));
   menu.add(new Dish(8,14.75, "Gespar" , 5));
   System.out.println("36 - " + menu);
   menu.add(new Dish(9,100.01, "Romulan Ale" , 5));
   System.out.println("37 - " + menu);
}

This is the Dish class:

public class Dish {

    private static int catalogNumber;
    private static Double costs;
    private static String nameofDish;

    public static int availabilityOfDish;

    public Dish(int number, Double price, String name, int availability) {
        catalogNumber = number;
        costs = price;
        nameofDish = name;
        availabilityOfDish = availability;
    }

    public static int getCatalogNumber() {
        return catalogNumber;
    }

    public static Double getCosts() {
        return costs;
    }

    public static String getNameofDish() {
        return nameofDish;
    }

    public static int getAvailabilityOfDish() {
        return availabilityOfDish;
    }

    @Override
    public String toString() {
        return catalogNumber + " " + costs + " " + nameofDish + " " +   
availabilityOfDish;
    }
}

Does this happen because of my declaration?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Lukas
  • 87
  • 5

2 Answers2

1

It happens because you use static keyword in the class Dish. static means it's a common value for all the instances of the class regardless their count - simply said. Thus these methods and variables are not bound to a certain instance.

Omit all the static keywords and it should work well.

Read more about static at the official documentation:

Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
0

Change Dish class to this one (no static) :

public class Dish {

    private int catalogNumber;

    private Double costs;

    private String nameofDish;

    public int availabilityOfDish;

    public Dish(int number, Double price, String name, int availability) {
        catalogNumber = number;
        costs = price;
        nameofDish = name;
        availabilityOfDish = availability;
    }

    public int getCatalogNumber() {
        return catalogNumber;
    }

    public Double getCosts() {
        return costs;
    }

    public String getNameofDish() {
        return nameofDish;
    }

    public int getAvailabilityOfDish() {
        return availabilityOfDish;
    }

    @Override
    public String toString() {
        return catalogNumber + " " + costs + " " + nameofDish + " " +   
    availabilityOfDish;
    }
    }
sharon182
  • 533
  • 1
  • 4
  • 19