java - Why is creating an instance of this object altering the instance variable of another object? -
i have defined bankaccount class, , created 2 different accounts extend bankaccount: savings account , checkings account. constructors have posted below:
public class timedepositaccount extends savingsaccount{ private int numberofmonths; private static final double withdraw_penalty = 20; timedepositaccount(double interestrate, int numberofmonths){ super(interestrate); this.numberofmonths = numberofmonths; } }
and savings account:
public class savingsaccount extends bankaccount { private static double interestrate; public savingsaccount(double interestrate){ super(); this.interestrate = interestrate; } }
in tester create savingsaccount, , timedeposit account:
savingsaccount momssavings = new savingsaccount(5); timedepositaccount collegefund = new timedepositaccount(10, 3);
after going through debugger, momssavings' interest rate set 5 specified, however, when create collegefund, program changes momssavings' interest rate 10, along creating collegefund object. tell me error is?
thank you.
you've declared interestrate static, there's 1 value across instances.
change non-static:
private double interestrate;
Comments
Post a Comment