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

1 Like

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

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

After some experimentation it seems to me that the issue is that you’re providing the information you want in the string, instead of a reference to a string object. I got an error when I tried to use two return commands, I’m guessing that after running the first return command, it returns to the main and the objects in the constructor method are no longer accessible, hence you can only have one return command.

Here’s how I made it work. You’ll notice that the \r creates a linebreak, so we don’t need two separate strings.

public String toString(){
String message = "Accessing Savings Account \rYou are now viewing your savings account. Your balance is " + balance;

return message;

}

What was it through ??

Hello everyone maybe i cant read directions or something, but i am really so confused on what to do on the first step each time i think i have it right there is always some sort of error! please help );

Hello, @hype1804869827. Welcome to the forum! Which exercise are you on specifically? If you can post a link to it, I’ll try to be of some assistance.

hello @midlindner i really appreciate you taking your time to help me ! I’m having trouble with step one of this project ! Simply just stuck maybe i cant understand the directions or something.

This instruction?

In between the constructor and the main() method, add a method called advertise() to the Store class. It should be accessible by other classes, and should have no output.

You can leave the body of the method empty.

I’m sorry, i’m not being clear enough it’s the 9th project aka the " Learn Java: Methods - Review " where we are building our own savings account, but the very first step on this one.
Dont know why i keep getting this error i know it has to do with maybe a missing “(” or “{” but not sure if maybe im overseeing something.
/SavingsAccount.java:31: error: illegal start of expression
public void checkBalance() {
^

1 Like

Ah. Okay. Got it. First, when you post code, it will help me if you click the </> button first, and then paste your code in the space provided. That way I can see how everything is formatted, and even copy and paste it, to run it myself, and help locate errors. Where did you locate the function declaration, and do you have a closing }? The empty function should look like this:

public void checkBalance() {

}

From the error, it appears you may have placed the method inside of the main() method. It needs to be outside the method.

You wont believe it, but i just figured it out haha im so sorry, but i honestly really do i appreciate you taking your time to help me out also will do for next time, rookie mistakes );

No worries. Glad you figured it out! :+1:

1 Like

And what that this mean? For me the two statement are the same. Plese be more specific.

1 Like

It’s because of the two return statements in your toString() method.

As the error message said the second return statement is “unreachable” because the first return already exits the method. When you delete or commenting-out one of the lines with a return statement the program will work.