Final Review of Learn Java: Methods - Correct answer with maddening error

https://www.codecademy.com/courses/learn-java/lessons/learn-java-methods/exercises/review

This is the exercise in question. First is the error code.

SavingsAccount.java:45: error: non-static method deposit(int) cannot be referenced from a static context
SavingsAccount.deposit(600);
^
SavingsAccount.java:48: error: non-static method checkBalance() cannot be referenced from a static context
SavingsAccount.checkBalance();
^
SavingsAccount.java:51: error: non-static method deposit(int) cannot be referenced from a static context
SavingsAccount.deposit(600);
^
SavingsAccount.java:54: error: non-static method checkBalance() cannot be referenced from a static context
SavingsAccount.checkBalance();
^
7 errors

Below is my code

public class SavingsAccount {

int balance;

public SavingsAccount(int initialBalance){

balance = initialBalance;

}

public void checkBalance() {

System.out.println("Hello!");
System.out.println("Your balance is " + balance);

}

public void deposit(int amountToDeposit) {

int newBalance = amountToDeposit + balance;

balance = newBalance;
System.out.println("You just deposited" + amountToDeposit);

}

public int withdraw(int amountToWithdraw) {

int newBalance = balance - amountToWithdraw;

balance = newBalance;
System.out.println("You just withdrew " + amountToWithdraw);
return amountToWithdraw;

}

public static void main(String args){

SavingsAccount savings = new SavingsAccount(2000);
//Check balance:
SavingsAccount.checkBalance();
//Withdrawing:
SavingsAccount.withdraw(300);
//Check balance:
SavingsAccount.checkBalance();
//Deposit:
SavingsAccount.deposit(600);
//Check balance:
SavingsAccount.checkBalance();
//Deposit:
SavingsAccount.deposit(600);
//Check balance:

SavingsAccount.checkBalance();
}
}

Hello! Since your deposit()(and, by the looks of it, none of your methods) are static, which means you must first create an instance of the SavingsAccount class, before you can call them:

SavingsAccount sa = new SavingsAccount();//do you need to pass parameters in here?
sa.whicheverMethodYouWantToCall();

Alternatively, you could make your methods static methods. See the article I linked above.

Hello,

So in your main method it looks like you have a slight “typo” if you will. Your construct is named SavingsAccount, your instance that you’ve created is named “savings.”

When you’re manipulating your instance you need to call on the specific instance, not the Construct.

EX: savings.checkBalance();

I think that may be the issue, please let me know if this helped or if I am wrong. :slight_smile:

EDIT: Please see this cheat sheet for an overview: Learn Java: Object-Oriented Java Cheatsheet | Codecademy

1 Like

Ah, I see, and it worked. I had used the class name “SavingsAccount” instead of my newly created object name “savings” to invoke the methods. I switched this around, using “savings” to call the methods, and it worked!

Thanks so much,

Connor

Yes, I didn’t realize which object name to use, “savings” or “SavingsAccount”. It’s pretty obvious now though, I have to use “savings” as this is the specific object and not the construct.

Feels so good to figure this out, thank you!