简体   繁体   中英

Java - Static Variables

  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;
    }

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);

    }
}

Solution summarized:

static variable:

private static double totalBalance;

constructor 1:

totalBalance += this.balance;

others:

totalBalance += amount;

totalBalance -= (amount + fee);

totalBalance += (this.balance * INTEREST_FEE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM