このコードが正しくフォーマットされていない場合は、事前にお詫びし、各行を再入力する代わりに貼り付けを試みます。それが正しくない場合、誰かが一度に複数行のコードを貼り付ける簡単な方法を教えてもらえますか?ヘッダから結果なしで、Cannot make a static reference to the non-static field balance.
は静的でないフィールドへの静的な参照を作成できません
私は方法は、静的な作り試してみました、と「静的」除去することにより、メインメソッド非静的を作る:
私の主な質問は、私はというエラーメッセージが出続けるということですしかし、私はメッセージを得ます: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;
}
}
私は「バランス_knows_アカウントので、最も簡単な答えはメソッドからそれらを削除することです。よく分かりませんあなたが本当にそれらを必要とする場合は、main()。 – user949300
からの呼び出しでaccount.balanceと言う必要があります。スペースを変更タブを書式設定し、ペーストしてctrl-kを押すとコードを選択します –