Write Methods

Write reusable methods to organize logic.

Let’s give your Java code some structure and reusability. In this lesson, you’ll learn how to write your methods—reusable blocks of logic.

Goal

You’ll aim to:

  • Write and call methods.

  • Understand void vs. return methods.

  • Clean up your main method.

Define a method

To define a method, you need to specify:

  1. Access modifier (e.g., public, private): Determines the method’s visibility.

  2. Return type (e.g., void, int, String): Defines what the method will return, if anything.

  3. Method name: The name used to call the method.

  4. Parameters (optional): Variables that allow you to pass information into the method.

  5. Body: The block of code that gets executed when the method is called.

Get hands-on with 1400+ tech skills courses.