Click here to navigate the course.

Drag the edges to resize the window.

In Projects, you can keep track of your progress as you go throught the tasks. Check each item as you complete it!

Code Editor

Let's explore one of the keywords used in declaring a method. In past exercises, when creating new methods, we used the keyword void.

The void keyword indicates that no value should be returned by the method after it executes all the logic in the method. If we do want the method to return a value after it finishes running, we can specify the return type.

  1. The void keyword (which means "completely empty") indicates to the method that no value is returned after calling that method.
  2. Alternatively, we can use data type keywords (such as int, char, etc.) to specify that a method should return a value of that type.

An example of indicating a return value for a method is below:

class Car { int modelYear; public Car(int year) { modelYear = year; } public void startEngine() { System.out.println("Vroom!"); } public void drive(int distanceInMiles) { System.out.println("Your car drove " + distanceInMiles + " miles!"); } public int numberOfTires() { return 4; } public static void main(String[] args){ Car myFastCar = new Car(2007) myFastCar.startEngine();; int tires = myFastCar.numberOfTires(); System.out.println(tires); } }

In the example above, we created the numberOfTires method. This method specifies that it will return an int data type. Inside of the method, we used the return keyword to return the value of 4 which is an int type.

Within main, we called the numberOfTires method on myFastCar. Since the method returns an int value of 4, we store the value within an int variable called tires. We then print the value of tires to the console.

Report a Bug
If you see a bug or any other issue with this page, please report it here.