class Item {
private String itemName;
private static int counter = 0;
public void getItem(String _itemName) {
itemName = _itemName;
counter++;
}
public void showItem() {
System.out.println("Item Name: " + itemName + "\tQuantity: " + counter);
}
public static int getCounter() {
return counter;
}
}
class Program {
public static void main(String[] s) {
try {
Item i1 = new Item();
i1.getItem("BMW XM Label Red");
i1.showItem();
Item i2 = new Item();
i2.getItem("Desktop PC");
i2.showItem();
Item i3 = new Item();
i3.getItem("Laptop");
i3.showItem();
System.out.println("Total objects created: " + Item.getCounter());
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
/*
run:
Item Name: BMW XM Label Red Quantity: 1
Item Name: Desktop PC Quantity: 2
Item Name: Laptop Quantity: 3
Total objects created: 3
*/