Defining and Calling Methods
Explore how to define and call methods in Java to structure your programs into reusable, focused actions. Understand method syntax, parameters, return types, and execution flow to build modular, maintainable applications that avoid monolithic code blocks and improve readability.
We'll cover the following...
So far, all code has been written inside a single main method. Although this approach works for small programs, it becomes difficult to maintain as the codebase grows. In a production application, operations such as tax calculation, email dispatch, and user validation should not be implemented in a single monolithic block. This structure reduces readability and significantly increases maintenance overhead.
To solves this, we rely on Java methods. Think of them as the “verbs” of our code. They allow us to define an action once and perform it as many times as we need. By breaking our code into smaller, focused methods, we turn a chaotic list of instructions into a clean, readable story.
The anatomy of a method
A method is a named block of code that performs a specific task. Defining a method involves specifying its access level, return type, name, and any inputs it requires.
For now, we will define our methods as public static. We will explore access modifiers like public and private in depth later, but we need static here so our methods can be called directly from the main method.
Here is the syntax for a basic method:
Return type: Indicates what data the method sends back.
voidmeans it returns nothing.Method name: Follows camelCase conventions. While any valid identifier works, best practice dictates using a verb-noun pair that clearly describes what the method does (e.g.,
calculateTotal,sendEmail).Parameters: A list of variable(s) inside parentheses that receive input values. If no input is needed, the parentheses remain empty.
Body: The code block
{ ... }that executes when the method is called.
Invoking methods
Defining a method does not run it. To execute the code inside a method, we must call (or invoke) it. When we call a method, the program's control flow "jumps" from the current line to the method's body. Once the method finishes, control returns to exactly where it left off.
We can call a method as many times as we want, reusing the logic without rewriting it.
Lines 4–7: We define
printGreeting. It takes no inputs and returns nothing (void).Line 13: The program pauses execution of
mainand jumps to line 4 to runprintGreeting.Line 15: After
printGreetingfinishes, execution resumes here.Line 18: We call the method again, reusing the code defined in lines 4–7.
Passing data with parameters
A method that always does the exact same thing is limited. To make methods dynamic, we define parameters. Parameters act like variables declared inside the method’s parentheses.
When we call the method, we provide the actual values, known as arguments. The Java compiler ensures that the type of the argument matches the type of the parameter.
Line 4: We define two parameters:
int widthandint height. These variables only exist inside this method.Line 5: We use the parameters
widthandheightexactly like local variables to calculate the area.Line 6: We print the formatted result to the console.
Line 11: We call the method with arguments
5and10. Inside the method,widthbecomes 5 andheightbecomes 10.Line 16: We pass variables
wandh. Their values (7 and 3) are copied into the method’s parameters.
Returning values
Often, we need a method to calculate a result and give it back to the code that called it. We do this by changing the return type from void to a specific data type (like int, double, or String) and using the return keyword.
The return statement immediately terminates the method and sends the specified value back to the caller.
Lines 4–7: We define the
calculateSquaremethod to compute the square of a number. Theintkeyword indicates it will return an integer result.Line 6: The
returnstatement immediately ends the method and sends the calculatedresultback to the caller.
Lines 10–16: The
isEvenmethod evaluates if a number is divisible by 2. It returns aboolean(trueorfalse) to report the outcome.Line 20: We call
calculateSquare(4). The method runs, calculates 16, and returns it to be stored in the variablesquareVal.Line 24: We call
isEven(squareVal)to check the number. Since 16 is even, the method returnstrue, causing theifblock to execute.
Method execution flow
Methods can call other methods. This allows us to build complex logic by layering simple operations. When a method calls another, the new method is pushed onto the call stack; a temporary memory structure that tracks execution. The called method must finish completely before the previous method resumes.
Line 20: The
maincallsprocessUser("Ai"). Control moves to line 3.Line 5:
processUser()pauses and callsvalidateUser(). Control moves to line 14.Line 16:
validateUser()returnsfalse(length is 2). Control returns to line 5.Line 7:
processUser()resumes and executes theelseblock.
Line 21:
processUser("Java")begins the cycle again.
Let's visualize exactly how Java manages these method calls in memory as the program runs:
By breaking our program into these small, interacting components, we move from writing monolithic scripts to building modular applications. This approach reduces redundancy, isolates bugs, and makes our code readable.