无法对非静态字段进行静态引用

如果此代码格式不正确,我会提前道歉,尝试粘贴而不是重新输入每一行。如果它不对,有人可以告诉我一次粘贴多行代码的简单方法吗?

My main question is that I keep getting an error message stating: Cannot make a static reference to the non-static field balance.

I have tried making the methods static, with no result, and making the main method non-static by removing "static" from the header, but then I get the message: java.lang.NoSuchMethodError: main Exception in thread "main"

有没有人有任何想法?任何帮助表示赞赏。

public class Account {

    public static void main(String[] args) {
        Account account = new Account(1122, 20000, 4.5);

        account.withdraw(balance, 2500);
        account.deposit(balance, 3000);
        System.out.println("Balance is " + account.getBalance());
        System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
        System.out.println("The account was created " + account.getDateCreated());
    }

    private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;
    public java.util.Date dateCreated;

    public Account() {
    }

    public Account(int id, double balance, double annualInterestRate) {
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

    public void setId(int i) {
        id = i;
    }

    public int getID() {
        return id;
    }

    public void setBalance(double b){
        balance = b;
    }

    public double getBalance() {
        return balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double interest) {
        annualInterestRate = interest;
    }

    public java.util.Date getDateCreated() {
        return this.dateCreated;
    }

    public void setDateCreated(java.util.Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public static double withdraw(double balance, double withdrawAmount) {
        double newBalance = balance - withdrawAmount;
        return newBalance;
    }

    public static double deposit(double balance, double depositAmount) {
        double newBalance = balance + depositAmount;
        return newBalance;
    }   
}
7
我不确定为什么你甚至将余额作为account.withdraw()和account.deposit()方法的参数。由于帐户知道余额,最简单的答案是将它们从或者,如果你真的想要它们,你需要在main()的调用中说出account.balance。
额外 作者 user949300,
至于格式化更改选项卡到空格并在粘贴后选择代码并按ctrl-k自动缩进
额外 作者 ratchet freak,

6 答案

main is a static method. It cannot refer to balance, which is an attribute (non-static variable). balance has meaning only when it is referred through an object reference (such as myAccount.balance or yourAccount.balance). But it doesn't have any meaning when it is referred through class (such as Account.balance (whose balance is that?))

我对您的代码进行了一些更改,以便进行编译。

public static void main(String[] args) {
    Account account = new Account(1122, 20000, 4.5);
    account.withdraw(2500);
    account.deposit(3000);

和:

public void withdraw(double withdrawAmount) {
    balance -= withdrawAmount;
}

public void deposit(double depositAmount) {
    balance += depositAmount;
}   
11
额外

线条

account.withdraw(balance, 2500);
account.deposit(balance, 3000);

你可能想要撤回并存入非静态,并让它修改余额

public void withdraw(double withdrawAmount) {
    balance = balance - withdrawAmount;
}

public void deposit(double depositAmount) {
    balance = balance + depositAmount;
}   

并从通话中删除balance参数

6
额外

您试图直接从静态方法访问非静态字段,这在java中是不合法的。 balance是一个非静态字段,因此要么使用对象引用访问它,要么使其静态。

1
额外

静态调用撤回和存款是您的问题。         account.withdraw(余额,2500); 此行无法使用,因为“balance”是Account的实例变量。无论如何,代码没有多大意义,不会将提取/存入封装在Account对象本身内?所以退出应该更像

public void withdraw(double withdrawAmount)
{
    balance -= withdrawAmount;
}

当然,根据您的问题,您可以在此处进行额外验证以防止负余额等。

1
额外

如果你愿意,你可以保持你的提款和存款方法是静态的,但是你必须像下面的代码那样写。 sb =起始余额和eB =期末余额。

Account account = new Account(1122, 20000, 4.5);

    double sB = Account.withdraw(account.getBalance(), 2500);
    double eB = Account.deposit(sB, 3000);
    System.out.println("Balance is " + eB);
    System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
    account.setDateCreated(new Date());
    System.out.println("The account was created " + account.getDateCreated());
1
额外

写吧:

private static double balance = 0;

你也可以这样写:

private static int id = 0;
private static double annualInterestRate = 0;
public static java.util.Date dateCreated;
0
额外