Write Methods
Write reusable methods to organize logic.
We'll cover the following...
We'll cover the following...
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
voidvs. return methods.Clean up your main method.
Define a method
To define a method, you need to specify:
Access modifier (e.g.,
public,private): Determines the method’s visibility.Return type (e.g.,
void,int,String): Defines what the method will return, if anything.Method name: The name used to call the method.
Parameters (optional): Variables that allow you to pass information into the method.
Body: The block of code that gets executed when the method is called.