I'm writting an application which should be extremely simple but it keeps using the last set values for name
and boxesSold
for everything. Here's a minimal example:
public class BandBoosterDriver
{
public static void main (String[] args)
{
BandBooster booster1 = new BandBooster("First");
BandBooster booster2 = new BandBooster("Second");
booster2.updateSales(2);
System.out.println(booster1.toString());
System.out.println(booster2.toString());
}
}
and here is the problem class:
public class BandBooster
{
private static String name;
private static int boxesSold;
public BandBooster(String booster)
{
name = booster;
boxesSold = 0;
}
public static String getName()
{
return name;
}
public static void updateSales(int numBoxesSold)
{
boxesSold = boxesSold + numBoxesSold;
}
public String toString()
{
return (name + ":" + " " + boxesSold + " boxes");
}
}
This produces
Second: 2 boxes
Second: 2 boxes
But I would expect
First: 0 boxes
Second: 2 boxes
How can I get it to work the way I expect it to?