0

in this code i need to make an object of the extended class basicaccount, but i get the error message "Non-static variable cannot be referenced from a static context" what can i do better?

public class BankAccount {
  private double balance;

  public BankAccount() {
    balance = 0;
  }

  public BankAccount(double initialBalance) {
    balance = initialBalance;
  }
  public void deposit(double amount) {
    double newBalance = balance + amount;
    balance = newBalance;
  }
   public void withdraw(double amount) {
    double newBalance = balance - amount;
    balance = newBalance;
  }

  public double getBalance() {
    return balance;
  }
class BasicAccount extends BankAccount {
    public BasicAccount(Double d) {
      balance = d;
    }
}
  class Main {
    public static void main(String args[]) {
      BankAccount account = new BasicAccount(100.00);
      double balance = account.getBalance(); //expected 100.00;
      account.withdraw(80.00);
      balance = account.getBalance(); //expected 20.00;
      account.withdraw(50.00);
      balance = account.getBalance(); //expected 20.00 because the amount to withdraw is larger than the balance
    }
  }
}
Shaw1234
  • 21
  • 1
  • Can you mark the line where have warning? – KunLun Mar 10 '20 at 14:37
  • 2
    You seem to have put your `BasicAccount` class inside your `BankAccount` class, making it an inner class. That means you can't instantiate a `BasicAccount` without an existing instance of `BankAccount`. You probably didn't want to do that. Avoid putting classes inside other classes before you know what you're doing. – khelwood Mar 10 '20 at 14:39
  • Does this answer your question? [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Bashir Mar 10 '20 at 14:47

3 Answers3

0

You seem to have put your BasicAccount class inside your BankAccount class, making it an inner class. That means you can't instantiate a BasicAccount without an existing instance of BankAccount.

You probably didn't want to do that. I suggest that you avoid putting classes inside other classes before you understand the effects.

Move your BasicAccount and Main classes outside of the definition of the BankAccount class.

khelwood
  • 55,782
  • 14
  • 81
  • 108
0

Non-static nested classes (BasicAccount and Main) cannot be instantiated without the encapsulating BankAccount object. To instantiate BasicAccount you need to call new BasicAccount(...) on an existing BankAccount object.

BankAccount account = new BankAccount().new BasicAccount(0);

However I think you may not have wanted this - if that's the case I think you should just move BasicAccount and Main from inside BankAccount.

0
  1. Make Main your public class.
  2. Remove public from BankAccount class
  3. Move your Main class outside of your BankAccount class.
  4. Make certain the static main(String[] args) is in the Main public class.

Here is what it should look like

class BankAccount {
    private double balance;

    public BankAccount() {
        balance = 0;
    }

    public BankAccount(double initialBalance) {
        balance = initialBalance;
    }

    public void deposit(double amount) {
        double newBalance = balance + amount;
        balance = newBalance;
    }

    public void withdraw(double amount) {
        double newBalance = balance - amount;
        balance = newBalance;
    }

    public double getBalance() {
        return balance;
    }

    class BasicAccount extends BankAccount {
        public BasicAccount(Double d) {
            balance = d;
        }
    }
}

public class Main {
    public static void main(String args[]) {
        BankAccount account = new BasicAccount(100.00);
        double balance = account.getBalance(); // expected 100.00;
        account.withdraw(80.00);
        balance = account.getBalance(); // expected 20.00;
        account.withdraw(50.00);
        balance = account.getBalance(); // expected 20.00 because the amount to withdraw is larger than the balance
    }
}

WJS
  • 36,363
  • 4
  • 24
  • 39