2015-01-20 2 views
-2

이 코드의 16 행에 오류가 있습니다. 정적에 대한 무언가, 그것을 이해할 수 없다, 어떤 아이디어가 도움이 될 것입니다.정적이 아닌 변수 이것은 정적 컨텍스트 라인에서 참조 할 수 없습니다. 16

package sample1; 

import java.util.ArrayList; 
import java.util.Date; 


public class Sample1 { 
    //main class 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     Account acc1 = new Account("George", 1122, 1000, .015); 



     System.out.print("The account holder's name: " + acc1.getname() 
       + ". The annual interest rate: " + acc1.getrate(.015) 
       + ". The balance: " + acc1.getbalance() + ". The monthly interest: " 
       + acc1.monthlyInterest + ". All the transactions: " 
       + acc1.getTransactions()); 
    } 


    public class Transaction { 

     private Date datetrans; 
     private char type; 
     private double amount; 
     private double balance; 
     private String description; 
     char Withdraw = 'W'; 
     char deposit = 'D'; 

     public Transaction(Date newDateTrans, char newType, double newAmount, 
       double newBalance, String newDescription) { 
      datetrans = newDateTrans; 
      type = newType; 
      amount = newAmount; 
      balance = newBalance; 
      description = newDescription; 
      datetrans = new Date(); 
     } 

     Date getdatetrans() { 
      return datetrans; 
     } 

     char gettype() { 
      return type; 
     } 

     double getamount() { 
      return amount; 
     } 

     String getdescription() { 
      return description; 
     } 

     double getbalance() { 
      return balance; 
     } 

    } 




     public class Account { 

      private int id = 0; 
      private String name; 
      private double balance = 0; 
      private double rate = 0; 
      private Date dateAdded; 
      private ArrayList<String> transactions = new ArrayList(); 

      public Account(String newName, int newId, double newBalance) // this is where the error is, any ideas why it can be { 
       id = newId; 
       name = newName; 
       balance = newBalance; 
       dateAdded = new Date(); 
      } 

      int getid() { 
       return id; 
      } 

      double getbalance() { 
       return balance; 
      } 

      double getrate(double newRate) { 
       return rate; 
      } 

      public void setid(int newId) { 
       id = newId; 
      } 

      public void setbalance(double newBalance) { 
       balance = newBalance; 
      } 

      public void setrate(double newRate) { 
       rate = newRate; 
      } 

      String getname() { 
       return name; 
      } 

      String getTransactions() { 
       return transactions.toString(); 
      } 

      Date getDateAdded() { 
       return dateAdded; 
      } 
      double monthlyInterestRate; 
      double monthlyInterest; 
      String withdraw; 
      String deposit; 

      public double getMonthlyInterestRate() { 
       monthlyInterestRate = rate/12; 
       return monthlyInterestRate; 
      } 

      public double getMonthlyInterest() { 
       monthlyInterest = balance * monthlyInterestRate; 
       return monthlyInterest; 
      } 

      String getDeposit() { 
       return deposit; 
      } 

      void addtransactions(String deposit) { 
       transactions.add(deposit); 
      } 

      String getWithdraw() { 
       return withdraw; 
      } 

      void droptransactions(String withdraw) { 
       transactions.remove(withdraw); 
      } 
     } 
    } 
+1

에 당신이 당신의 클래스 선언의 예에 정적 키워드를 넣어야 할이 issuue를 해결합니다. –

+0

실제 필요가 없으면 클래스를 중첩하지 마십시오. 이것이 바로 여기 오류의 원인입니다. –

+0

어떻게해야합니까? – jose

답변

1

이동 트랜잭션과 자신의 파일에 계정이 sample1을 분리하지만, (클래스 이름과 일치하는 파일 이름에는 sample1과 같은 디렉토리에 상단에 같은 패키지 선언,와) 같은 패키지이다.

중첩 된 클래스 선언은 (외부로 선언 된 경우를 제외하고) 외부 클래스에 대한 참조를 사용하는 내부 클래스를 만듭니다. 그것들은 당신이 여기서 필요로하지 않는 복잡성입니다. (은 내부 클래스의 선언에 정적을 추가 할 수 있지만 간단하게 유지하는 것이 좋습니다.)

+0

나는 그 (것)들을 그들 자신의 파일로 옮겼다. 그러나 나는이 줄에서 아직도 오류를 얻는다. – jose

+0

Account acc1 = new Account ("George", 1122, 1000, .015); – jose

+0

@jose : Account에 대한 생성자는 3 개의 인수 만 사용하며 4를 전달합니다. –

1

Account이라는 중첩 비 정적 클래스가 있습니다. 즉,이 클래스의 인스턴스를 만들려면이 경우 Sample1 인 외부 클래스의 인스턴스가 있어야합니다. 세 가지 방법으로 해결할 수 있습니다.

1) 외부 클래스

Account acc1 = new Sample1().new Account("George", 1122, 1000, .015); 

2)의 인스턴스를 사용하거나 외부 클래스를 필요로 나타나지 않기 때문에 당신은 클래스가 정적 만들 수 있습니다.

static class Account { 

트랜잭션에 대해 동일한 작업을 수행 할 수 있습니다.

3) 중첩 된 클래스를 사용하지 말고 한 단계 위로 이동하십시오. IDE가이 리펙터를 사용할 수 있습니다.

환경 설정이 없으면 최소한의 변경 사항이 있으므로 옵션 2를 사용하고 이것이 의도 한 것일 가능성이 큽니다.

1

정적 변수 메모리는 클래스가로드 될 때 처음으로 할당되기 때문에 정적 변수를 사용할 수 없습니다. 코드에서 인스턴스를 만들기 위해 런타임에 할당 된 정적 main() 메소드가 비 정적.

Here Sample1 main function donot know Account class exist or not.는 내부 클래스에 최대 읽기

static class Account{} 

OUTPUT

관련 문제