简体   繁体   中英

Non-static variable cannot be referenced from a static context problem

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
    }
  }
}

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.

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 .

  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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM