2013-11-01 7 views
0

계정 이름으로 클래스를 만들었습니다. 그런 다음 유형 클래스의 객체를 인스턴스화합니다. 모든 코드는 TestAccount 파일에 저장됩니다. testaccount.TestAccount.main (TestAccount.java:8) 에서 "주"스레드에서클래스를 인스턴스화 할 수 없습니다.

예외 아래 java.lang.ExceptionInInitializerError 을 표시에 의해 발생하지만 시스템은 나에게 오류를 제공합니다 : java.lang.RuntimeException가를 : Uncompilable 소스 코드 - 클래스 계정 공개, testaccount.Account에서 Account.java 라는 이름의 파일에 선언해야한다 (TestAccount.java:20) ... (1 개) 더 자바 결과 : 1

. 아래는 내 코드입니다.

package testaccount; 

public class TestAccount 
{ 
public static void main(String[] args) 
{ 

Account Account1=new Account(); 
Account1.setId(1122); 
Account1.setBalance(20000); 
Account1.setAnnualInterestRate(4.5); 
System.out.println("The monthly interest rate is " + Account1.getMonthlyInterestRate()); 
System.out.println("The balance after the withdrawal is "+ Account1.withdraw(2000)); 
System.out.println("The balabce after the deposit is " + Account1.deposit(3000)); 

} 

} 

public class Account 
    { 
     private int id; 
     private double balance; 
     private double annualInterestRate; 
     private static long dateCreated; 

     public Account() 
     { 
      id=0; 
      balance=0; 
      annualInterestRate=0; 
      dateCreated=System.currentTimeMillis(); 
     } 

     public Account(int newId,double newBalance) 
     { 
      id=newId; 
      balance=newBalance; 
     } 

     public int getId() 
     { 
      return id; 
     } 
     public void setId(int newId) 
     { 
      id=newId; 
     } 

     public double getbalance() 
     { 
      return balance; 
     } 
     public void setBalance(double newBalance) 
     { 
      balance=newBalance; 
     } 

     public double getAnnualInterestRate() 
     { 
      return annualInterestRate; 
     } 
     public void setAnnualInterestRate(double newAnnualInterestRate) 
     { 
      annualInterestRate=newAnnualInterestRate; 
     } 

     public static long getDateCreate() 
     { 
      return dateCreated; 
     } 

     public double getMonthlyInterestRate() 
     { 
      return (annualInterestRate/12); 
     } 

     public double withdraw(double newWithdraw) 
     { 
      return (balance-newWithdraw); 
     } 

     public double deposit(double deposit) 
     { 
      return (balance+deposit); 
     } 
} 

내가 잘못하고있는 것을 말해 줄 수 있습니까?

+1

단일 Java 파일에 2 개의 분리 된 공용 클래스를 사용할 수 없습니다. –

+0

오류 메시지는 다음과 같은 힌트를 제공합니다 :'class Account is public, Account.java라는 파일에 선언되어야 함' – ajp15243

+0

그러면 두 파일을 어떻게 연결 할 수 있습니까? Account.java와 Testaccount.java? – user2918968

답변

1

Account.java이라는 새 파일을 만들어 계정 클래스를 만들어야합니다. 이것은 다른 클래스의 계정을 호출 할 때 jvm이 Account.class을 찾고 계정 클래스가 TestAccount.class이라는 파일에 있으면 찾을 수 없기 때문입니다.

그렇지 않으면 컴파일러는 않을만큼 모두 수업이 같은 패키지 (폴더)에있는로

파일

을 컴파일 당신은 "링크"두 개의 특별한 아무것도 할 필요가 없습니다.

물론 클래스를 중첩하지 않으려면 Account 클래스를 TestAccount 클래스 안에 넣어야합니다. 매우 지저분하므로 권장하지는 않습니다.

+0

새 Java 파일을 만들 필요는 없습니다. 그는'TestAccount' 클래스 내에'Account' 클래스를 정의 할 수 있습니다. –

+0

대답이 업데이트되었습니다. 코딩 방법이 좋지 않고 지저분하다는 것을 알게되었습니다. – Epicblood

0

좋은 해결책은 아니며 이미 제시된 해결책이 더 낫지 만 TestAccount에서 Account 클래스를 옮기고 정적 (클래스 정의 앞에 정적으로 지정)으로 만들 수 있습니다. 그것도 효과가있을 것입니다.

관련 문제