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();
}
The methods are instance methods – they operate on an instance of your class
SavingsAccount.When you call
SavingsAccount.printAccountInfo(), you are telling Java to callprintAccountInfo()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
SavingsAccountwhose account info you want to print. Let’s say this instance is in variablex, then you’d callx.printAccountInfo().The same thing happens in your call to
accrueInterest.