I have class ServiceCard with the field private int cardID
, and when I make one or more instances of ServiceCard, I want the first instance have cardID=1
, the second instance have cardID=2
, etc.
I try to make cardID to static, but it didn't work.
public class ServiceCard {
private static int cardID;
public ServiceCard() {
setCardID(getCardID() + 1);
}
public static int getCardID() {
return cardID;
}
public static void setCardID(int cardID) {
ServiceCard.cardID = cardID;
}
}
When I make a test :
ServiceCard card = new ServiceCard();
System.out.println(card.getCardID());
ServiceCard card2= new ServiceCard();
System.out.println(card2.getCardID());
System.out.println(card.getCardID());
Console:
1
2
2