I'm working on a classic homework program and cannot for the life of me figure out why my static variable in the superclass reacts the way it does..
The program is a bankaccount where I have created a superclass, Account, and two subclasses, CreditAccount and SavingsAccount.
public abstract class Account {
private double balance;
private int accountId;
**private static int lastAssignedNumber = 1000;** <--- the static int
private String accountType;
public Account (double q_balance, String q_accountType)
{
balance = q_balance;
accountType = q_accountType;
**accountId = ++lastAssignedNumber; <------ counter for new accountId**
}
)
public class CreditAccount extends Account {
public CreditAccount(double balance)
{
super(balance, "Creditaccount");
}
}
public class SavingsAccount extends Account {
public SavingsAccount(double balance)
{
super(balance, "Savingsaccount");
}
}
Previously, without subclasses when Account was the only object, the counter worked beautifully. But now when I create some new objects of savingsaccount and creditaccounts the program acts really weird and returns accountnumbers as follows:
new SavingsAccount(0); // **1001**
new CreditAccount(0); // **1001**
new CreditAccount(0); // **1002**
new SavingsAccount(0); // **1003**
new CreditAccount(0); // **1002**
new CreditAccount(0); // **1004**
new SavingsAccount(0); // **1005**
What in gods name is happening?! What am I missing? Shouldn't the two subclasses provoke the same static variable 'lastAssignedNumber' and add to it accordingly??
Kindest regards // Gewra