2011-11-12 4 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의 인스턴스 변수이므로이 행은 작동하지 않습니다. 어쨌든이 코드는별로 의미가 없지만 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가 아닌 정적 메소드에서 직접 가져옴 자바의 걸. 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()); 
관련 문제