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();
}
}