dimanche 22 mars 2020

In Java I'm accessing One Class in Another Class . But it gives error ,why?

I have Two Class Account & AccountThread Class .

I'm Accessing Account Class in AccountThread Class.

I'm Getting error While Accessing the Account class in AccountThread class.

My Account Class

package ATM.packageATM;

public class Account {

    int balance=1000;
    int remaining=0;

    public void withDraw(int amount) {

        remaining=balance-amount;
        if(amount>0 && remaining>=0){
            System.out.println("Amount : "+amount+" is deducted Remaining : "+remaining
            +"Thread Name : "+Thread.currentThread().getName());
        }

    }

}

And My AccountThread Class

package ATM.packageATM;

public class AccountThread extends Thread {

    public Account account=new Account();

    public void run(){

        account.withDraw(100);

    }

    public static void execute(){

        for(int i=0;i<5;i++){
            AccountThread accountThread=new AccountThread();
            accountThread.setName(String.valueOf(i));
            accountThread.start();
        }

    }

    public static void main(String args[]) {

        execute();

    }

}

and I'm getting error like this

AccountThread.java:5: error: cannot find symbol
    public Account account=new Account();
           ^
  symbol:   class Account
  location: class AccountThread
AccountThread.java:5: error: cannot find symbol
    public Account account=new Account();
                               ^
  symbol:   class Account
  location: class AccountThread
2 errors

Why This Error occur

Since Both the class in Same Package....

Aucun commentaire:

Enregistrer un commentaire