2011-11-12 14 views
6

このコードが正しくフォーマットされていない場合は、事前にお詫びし、各行を再入力する代わりに貼り付けを試みます。それが正しくない場合、誰かが一度に複数行のコードを貼り付ける簡単な方法を教えてもらえますか?ヘッダから結果なしで、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; 
    } 
} 
+0

私は「バランス_knows_アカウントので、最も簡単な答えはメソッドからそれらを削除することです。よく分かりませんあなたが本当にそれらを必要とする場合は、main()。 – user949300

+1

からの呼び出しでaccount.balanceと言う必要があります。スペースを変更タブを書式設定し、ペーストしてctrl-kを押すとコードを選択します –

答えて

6

ライン

account.withdraw(balance, 2500); 
account.deposit(balance, 3000); 
あなたが預金非静的を撤回し、作り、それがバランス

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

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

を変更してみましょうし、呼び出しからバランスパラメータを削除する場合があります

1

引き出しと預金の静的呼び出しが問題です。 account.withdraw(balance、2500); "balance"はAccountのインスタンス変数なので、この行は機能しません。とにかくコードはあまり意味がありませんが、アカウントオブジェクト自体の中にカプセル化されていますか?そう

10

mainは静的メソッドであるなど、あなたが否定的なバランスを防ぐために、ここで追加の検証を行うことができ、あなたの問題に応じて、よりもちろん

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

ようにする必要があり取り下げます。これは属性(非静的変数)であるbalanceを参照することはできません。 balanceは、オブジェクト参照(例えば、myAccount.balanceまたはyourAccount.balanceなど)によって参照された場合にのみ意味を持ちます。しかし、それはクラスを介して参照されたときに意味がありません(Account.balance(そのバランスは?))

コンパイルするようにコードを変更しました。

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

と:

private static double balance = 0; 

を、あなたはまた、そのようなものを書くことができる::

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

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

だけ書く

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

あなたは非静的フィールドにアクセスしようとしていますleでない静的メソッドから直接galのjava。 balanceは非静的フィールドなので、オブジェクト参照を使用してアクセスするか、静的にします。

-1

あなたは、以下のコードのように書く必要がある場合は、退会と預金の方法を静的に保つことができます。 sb =開始残高とeB =終了残高。あなたもaccount.withdrawの引数としてバランス()とaccount.deposit()メソッドを持っている理由

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()); 
関連する問題