-4
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");
    }
}

On numOfAccounts it keeps saying cannot find symbol - variable numOfAccounts.

eckes
  • 9,350
  • 1
  • 52
  • 65
Aneeqa
  • 1
  • 1
  • 1
    There is no variable `numOfAccounts` and your IDE is telling you that. If you want to use it, then create a corresponding variable first. – Tom Feb 21 '15 at 17:27
  • how do i create a corresponding variable? – Aneeqa Feb 21 '15 at 17:28
  • This might help you: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html. Mind that you need a `static` variable. – Tom Feb 21 '15 at 17:29
  • possible duplicate of [What does a "Cannot find symbol" compilation error mean?](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – Radiodef Feb 22 '15 at 00:27
  • I lowercased the fields and removed the boilerplate comments to make it better readable. – eckes Feb 22 '15 at 03:27

2 Answers2

2

The error means that Java can't find numOfAccounts. And indeed, I can't find it either - you'll have to declare it as a static field if you want to access it like that:

public class AccountDemo {
    public static int numOfAccounts = 10;
    ...
    public static void main(String [] args) {
    ...

But before you do, make sure to understand the difference between static and instance variables.

runDOSrun
  • 8,482
  • 5
  • 38
  • 50
2

You are missing numOfAccounts in your class. And by look of it it should be static.

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");
    }
}
Beri
  • 10,277
  • 3
  • 24
  • 47
  • If numOfAccounts tellf os number of Account instances and not AccountDemo then move this constructor to Account class. Because I think that you wanted to create instances of AccountDemo in main. – Beri Feb 21 '15 at 17:32