-1
  1. If I wanna create a static variable inside this class, which should save the total amount of all accounts. Is this right the way I did it?
    Just put a code inside the constructor and good is.
    Should anyway only be inside a constructor, right?

  2. How can I print static variables so I am able to check it?


public class Account {

    private static double totalBalance = 0;

    private final double INTEREST_RATE = 0.015;
    private int acctNumber;
    private double balance;
    private String name;

    public Account(String name, int acctNumber, double initialBalance) {
        this.name = name;
        this.acctNumber = acctNumber;
        this.balance = initialBalance;
        this.totalBalance += this.balance;
    }

    public Account(String name, int acctNumber) {
        this.name = name;
        this.acctNumber = acctNumber;
        this.balance = 0.0;
        this.totalBalance += this.balance;
    }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Patrick
  • 13
  • 4
  • First off you'll want to move `this.balance = initialBalance` before you do `totalBalance += this.balance`. Otherwise you'll just be adding zero every time – GBlodgett Jan 06 '19 at 22:27
  • Nitpick: if `this.balance = 0.0;` then what is the point of `this.totalBalance += this.balance;`? Anyway if constructors can be executed in different threads then this.totalBalance += this.balance; may cause problems (research "race condition" problem). Solution for that would be using class like [AtomicDouble](https://stackoverflow.com/a/5505512). – Pshemo Jan 06 '19 at 23:12

2 Answers2

1

To much code for simple question. The main thing is keyword static when declaring the field in the class. Always remember that these fields are shared among all instances of the class. In other words, when some instance change the value of the static field it is reflected in the all other instances of that class. Here the simple code is better than the words:

class A {
    public static int x;
}

public class Helper {

    public static void main(String[] args) {
        A someA = new A();
        A.x = 0;

        A someOtherA = new A();
        A.x = 5;

        //uncomment next line and see what happens
        //someA.x = -55;

        System.out.println("x in someA = " + someA.x);
        System.out.println("x in someOtherA = " + someOtherA.x);
        System.out.println("x in all instances of A = " + A.x);

    }
}

EDIT: About the question can I put the static variable inside the constructor, try this:

class B{
    private static int x;
    public B(int x){
        B.x = x;
    }

    public int getX() {
        return x;
    }
}

public class Helper {

    public static void main(String[] args) {
        B bOne = new B(44);
        System.out.println(bOne.getX());

        B bTwo = new B(88);
        System.out.println(bTwo.getX());
        System.out.println(bOne.getX());

    }
}

EDIT two

Here is the sample code regarding your questions in the comments:

class Acc {
    public static int noOfAccounts;
    public static double totalBalance;

    public Acc(double balance) {
        //increase the number of accounts
        Acc.noOfAccounts++;
        //add the balance to totalBalance
        Acc.totalBalance += balance;
    }
}


public class Helper {

    //test
    public static void main(String[] args) {

        Acc aOne = new Acc(15.4);
        System.out.println("Acc.noOfAccounts = " + Acc.noOfAccounts);
        System.out.println("Acc.totalBalance) = " + Acc.totalBalance);

        Acc aTwo = new Acc(100.0);
        System.out.println("Acc.noOfAccounts = " + Acc.noOfAccounts);
        System.out.println("Acc.totalBalance) = " + Acc.totalBalance);

    }
}
zlakad
  • 1,314
  • 1
  • 9
  • 16
  • Of course, I treated just one thread, but in multi treads environments, there must be some adjustments... – zlakad Jan 06 '19 at 23:18
  • so a static variable can go inside a constructor and inside a getter. do you have a tipp for me to solve my problem the best way possible? i mean normally there is just a counter as static variable for the amount of accounts. that would be easy, but now i want to get all balance of all accounts inside 1 static variable. also another thing I thought, how could I use the static variable to assign accountnumbers for the different accounts. that should be possible right? – Patrick Jan 06 '19 at 23:19
  • Yep, everything is possible, but I wouldn't code in your way. First of all there can be some complications, for example. what if some account is 'destroyed'? Second, you probably want to hold all accounts in some collection, text file, data base, right? In that case, you can easily count the number of accounts, see what is the total balance, and so on... – zlakad Jan 06 '19 at 23:30
  • So I think I got it know. New Day, new Luck.^^ Static Variable: private static double totalBalance = 0; Constructor 1: this.balance = initialBalance; totalBalance += this.balance; Constructor 2: this.balance = 0.0; totalBalance += this.balance; And now I can check it with the getter: public static double getTotalBalance() { return totalBalance; } and the test in the other class: double totalBalance = Account.getTotalBalance(); System.out.println(totalBalance); correct? But how can I assign an account number with the static variable? :-/ – Patrick Jan 07 '19 at 13:10
  • nah still broken. – Patrick Jan 07 '19 at 13:26
  • Well I think I got it soon, but how I can assign an account number, I dont get it, sorry. – Patrick Jan 07 '19 at 13:40
  • Would you always leave the static variable without = 0; etc? just the variable without any value. – Patrick Jan 07 '19 at 13:55
  • Yes. In this case the answer is YES! B/c when you declare the variable of primitive type that variable in internally initialized to default value - int to 0, double to 0.0, etc. Try to print `Acc.noOfAccounts` and `Acc.totalBalance` **before** creating any account – zlakad Jan 07 '19 at 13:58
  • Okay I get it. So maybe I could add a random Generator for create the numbers with the static variable. Otherwise it is just linear 1,2,3,4. If I would use it for an Student class with MatrNr. that would be not soo good, aye. – Patrick Jan 07 '19 at 14:05
  • It's good to experiment. So, if you find this answer useful, consider to accept it. That way the other users may benefit from this. – zlakad Jan 07 '19 at 14:07
  • Yes, still have a question regarding my code with the Account and totalBlance. See below please... – Patrick Jan 07 '19 at 14:10
0

Solution summarized:

static variable:

private static double totalBalance;

constructor 1:

totalBalance += this.balance;

others:

totalBalance += amount;

totalBalance -= (amount + fee);

totalBalance += (this.balance * INTEREST_FEE);

Patrick
  • 13
  • 4