Please enable JavaScript to use CodeHS

Chapter 2

Using Objects

2.5 Calling a Non-void Method

Similar to how methods can take in values as parameters, methods can also return values.

Returning Results

Recall the addTen() method from the previous section:

public void addTen(int x)
{
  int xPlusTen = x + 10;
  System.out.println(xPlusTen);
}
Plain text

This method takes in a parameter, x, and adds 10 to it. Lastly, the program prints the value to the console.

But what if you wanted to store the value of x + 10? In the method above, x + 10 is stored into a local variable called xPlusTen. However, this variable is lost when the method is finished – it cannot be accessed outside of the method.

Using a return statement will allow you to pass a value back out of the method. That return value can be stored into a variable for later use.

Here’s how to rewrite addTen() to return a value instead of printing:

public int addTen(int x)
{
  int xPlusTen = x + 10;
  return xPlusTen;
}
Plain text

There are a few differences here.

First, the return statement on line 4 replaces the print statement and does not require parentheses. Second, take a look at the first line of the method: public int addTen(int x). Instead of void, you now use int. This tells the program that the addTen() method will return a value that is an int. This is called the return type of the method.

Non-void methods return a value that is the same type as the return type in the signature. To use the return value when calling a non-void method, it must be stored in a variable or used as part of an expression. Up to this point, you’ve only used the void return type, which indicates that the method does not return anything. Because the addTen() method will return an integer, you set the return type to int.

When returning a value, it’s important to note that that method is not printing the value to the console, but rather sending back to the method call statement. This is similar to how passing in a value as an argument does not print the value to the console.

Calling a Method With a Return Value

A return value by itself would not be very useful, given that it does not print to the console. Fortunately, you can store a method’s return value into a variable. Take a look at the following code:

int num = 7;
int tenAdded = addTen(num);
Plain text

First, you create a variable named num and initialize it to 7. Then, you create another variable called tenAdded. Notice that tenAdded is not given a normal value. Instead, you are setting it equal to addTen(num). This means that the tenAdded variable will hold the result of whatever the method call addTen(num) returns. You know that addTen(num) will return 17, so tenAdded will be 17.

Multiple Parameters With a Return Value

Return values work in many situations. For example, you can rewrite the add method from the previous section to return the sum instead of print it to the screen:

public int add(int x, int y)
{
    int sum = x + y;
    return sum;
}
Plain text

You can now call the method and store its return values

int sum = add(10, 90);
System.out.println(sum);
Plain text

which would print 100 to the console.

Rectangle

Desks in a Room

Activity Log

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    Suppose you have a class called Rollercoaster. The Rollercoaster class has a method called goingDown, partially defined below

    public boolean goingDown()
    {
        // code omitted
    }
    Java

    Which of the following statements correctly stores the return value of goingDown when it is called on the Rollercoaster object called ferrisWheel?

Exercise: Laundry Costs

The LaundryService class is completed for you, but you may want to look at it to make sure you understand how to use it.

Then write a program in the main method that asks the user to enter the sales tax rate as a decimal, the number of pounds of clothes needed to be laundered, and the number of shirts needed to be dry cleaned.

Create a LaundryService object to compute the cost of the laundered clothes and dry cleaned shirts. Use $2 for the cost of laundered clothes per pound. Use $3 for the cost per dry-cleaned shirt.

Print the total cost. Then use the method to compute the tax and print out the grand total including tax.

Here is an example of the output

Enter the sales tax rate: 
.08
How many pounds of clothes do you need to be laundered? 
10
How many shirts do you need to be dry cleaned? 
5
Total: 35.0
Grand Total: 37.8
Plain text