public class AccountDemo {
public static void main(String [] args) {
Account accountholder1, accountholder2, accountholder3;
accountholder1 = new Account(100, 10, 1000, "", "");
accountholder1.showData();
System.out.println(AccountDemo.numOfAccounts+" accounts opened");
accountholder2 = new Account(200, 20, 2000, "", "");
accountholder2.showData();
System.out.println(AccountDemo.numOfAccounts+" accounts opened");
accountholder3 = new Account(300, 30, 3000, "", "");
accountholder3.showData();
System.out.println(AccountDemo.numOfAccounts+" accounts opened");
}
}
在numOfAccounts
上,它会一直说cannot find symbol - variable numOfAccounts
。
答案 0 :(得分:2)
错误意味着Java无法找到numOfAccounts
。
事实上,我也找不到它 - 如果你想这样访问它,你必须将它声明为静态字段:
public class AccountDemo {
public static int numOfAccounts = 10;
...
public static void main(String [] args) {
...
但在此之前,请确保在静态变量和实例变量之间understand the difference。
答案 1 :(得分:2)
您的课程中缺少numOfAccounts。看看它应该是静态的。
public class AccountDemo {
static int numOfAccounts = 0;
AccountDemo(){
numOfAccounts++; //increment number of accounts
}
public static void main(String [] args) {
Account accountolder1, accountHolder2, accountHolder2;
accountolder1 = new Account(100, 10, 1000, "", "");
accountolder1.showData();
System.out.println(AccountDemo.numOfAccounts+" accounts opened");
accountolder2 = new Account(200, 20, 2000, "", "");
accountolder2.showData();
System.out.println(AccountDemo.numOfAccounts+" accounts opened");
accountolder3 = new Account(300, 30, 3000, "", "");
accountolder3.showData();
System.out.println(AccountDemo.numOfAccounts+" accounts opened");
}
}