Pclef
Pclef

Reputation: 55

Basic Java Program: non-static method cannot be referenced from a static context

I'm a beginning programmer trying to learn the basics of java. Basically, the methods printBankSummary() and accrueInterestAllAccounts() in the bank class are giving me this problem. Here are the codes:

public class Bank {

  private String name;
  private SavingsAccount [] accounts;
  private int totalAccounts;
  public static final int MAX_ACCOUNTS  = 20;

  public Bank(String name) {
    this.name = name;
    totalAccounts = 0;
    accounts = new SavingsAccount[MAX_ACCOUNTS];
}

  public void printBankSummary() {
   System.out.println("Bank name: " + getName());
   BankAccount.printAccountInfo(); //non-static method cannot be referenced from a static context error
  }

  public void accrueInterestAllAccounts() {
    SavingsAccount.accrueInterest(); //non-static method cannot be referenced from a static context error
  }

  public static void main (String args[]) {
    Bank x = new BankAccount("Java S&L");

    x.printBankSummary();
    x.accrueInterestAllAccounts();
  }

Upvotes: 0

Views: 650

Answers (3)

The methods are instance methods - they operate on an instance of your class SavingsAccount.

When you call SavingsAccount.printAccountInfo(), you are telling Java to call printAccountInfo() as a static method. You are basically telling Java: "you can find this method in the class SavingsAccount, and you don't need an instance of SavingsAccount to use it.".

What you probably want to do is find the instance of class SavingsAccount whose account info you want to print. Let's say this instance is in variable x, then you'd call x.printAccountInfo().
The same thing happens in your call to accrueInterest.

Upvotes: 1

AlexFZ
AlexFZ

Reputation: 1459

The easy answer: Due to nature of static types in Java, any method that references a static method/variable must also be static.

A workaround for this would be to separate your classes and test program, such that there is no 'public static void main(String args[]) method in your class's source files. You could then put the two methods causing you trouble inside of your test class, and modify their method declarations to be static.

If you do want your two problem methods to be instance methods, then you will need to crate a new instance of your classes and call them that way.

Upvotes: 0

Spencer H
Spencer H

Reputation: 489

 BankAccount.printAccountInfo();

is a static method (Accessed from the class) and therefore cannot be accessed unless the method that calls it is also static. instead of

 public void printBankSummary() {
       System.out.println("Bank name: " + getName());
       BankAccount.printAccountInfo();
      }

What about

    public void printBankSummary() {
       System.out.println("Bank name: " + getName());
//calls printAccountInfo on the instance that called printBankSummary()
       printAccountInfo();
      }

And for

public void accrueInterestAllAccounts() {
    SavingsAccount.accrueInterest();
  }

You can't call the Class.Method, what you want to do is

    public void accrueInterestAllAccounts() {
for(Account acc: Accountarr) {            
            acc.accrueInterest();
          }
}

Upvotes: 0

Related Questions