FAQ: Learn Java: Methods - Review

This community-built FAQ covers the “Review” exercise from the lesson “Learn Java: Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

In step one of this activity, where it asks you to make a checkBalance function, it throws an error that says, “Oops! The test returned an error. Maybe you have a syntax error, or a typo. See full error.” When I click “See full error” it says, “Cannot read property “0” of undefined.” What does this mean? What do I have to do to fix the error?

1 Like

Why on method deposit is VOID and on method withdraw is INT

  public **void** deposit(int amountToDeposit){
    balance = amountToDeposit + balance;
    System.out.println("You just deposited " + amountToDeposit);
  }
  
  public **int** withdraw(int amountToWithdraw){
    balance = balance - amountToWithdraw;
    System.out.println("You just withdrew " + amountToWithdraw);
    return amountToWithdraw;

The deposit method is not returning any value, which means the return on the method is VOID.

On the other hand, the withdraw method will return amountToWithdraw, an int, so the return on the method is an INT.

The program is telling me that I completed all the steps, but I am seeing errors in the return instead of my printed messages. For every method that I call from the main method, I get an error like the below:

SavingsAccount.java:31: error: non-static method checkBalance() cannot be referenced from a static context
** SavingsAccount.checkBalance();**

Here is my main method:
** public static void main(String args){
SavingsAccount savings = new SavingsAccount(2000);

SavingsAccount.checkBalance();

SavingsAccount.withdraw(300);

SavingsAccount.checkBalance();

} **

Am I writing something incorrectly?

Late response but this is because you are calling SavingsAccount rather than savings. The new savings account created in the main method is called savings so when checking the balance and whatnot you will want to call savings.checkBalance();

Hope this helps!

1 Like

Hey, so I am having trouble with step one of this project. I keep getting a dozen errors after I put my code in,

/*********************/
   public void checkBalance(){
    System.out.println("Hello!");
    System.out.println("Your balance is ");
    }
    /*********************/

I noticed in the hint that the method title was blue. I dont recall CC teaching how to do that unless you were starting off the code. But does that have something to do with it?

Edit: Never mind i found the issue i didnt follow the instructions

So I was playing around a bit on the “Review” section and I tried to add a toString() method. However, the program produces the following error:

SavingsAccount.java:26: error: unreachable statement
return "You are now viewing your savings account. Your balance is " + balance;
^
1 error

Here is my program. What am I doing wrong and how can I fix it?

/*********************/
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){
balance = balance + amountToDeposit;
System.out.println("You just deposited " + amountToDeposit);
}

public void withdraw(int amountToWithdraw){
balance = balance - amountToWithdraw;
System.out.println("You just withdrew " + amountToWithdraw);
}

public String toString(){
return “Accessing savings account…”;
return "You are now viewing your savings account. Your balance is " + balance;
}

public static void main(String args){
SavingsAccount savings = new SavingsAccount(2000);

//Check balance:
savings.checkBalance();

//Withdrawing:
savings.withdraw(300);

//Check balance:
savings.checkBalance();

//Deposit:
savings.deposit(600);

//Check balance:
savings.checkBalance();

//Deposit:
savings.deposit(600);

//Check balance:
savings.checkBalance();

System.out.println(savings);

}
}
/*********************/