Java Methods in Java
A method is a block of code containing a series of statements that runs only when it is called. Methods allow you to reuse code without repeating yourself (DRY principle), breakdown complex logic into manageable chunks, and return calculated results.
"A method is like a vending machine button: you press the "Dispense Water" button (call method), the machine executes its internal steps, and drops a bottle into the tray (returns value)."
Deep Dive: How It Works
Method Signature: `public static returnType methodName(parameters) { ... }`
The `void` keyword: Specifies that the method performs an action but does not return a value back.
The `return` keyword: Passes a value back to the caller and terminates method execution.
Why `static`? A static method belongs to the class and can be invoked directly from `main` without creating an object instance.
Syntax Blueprint
public static int addNumbers(int a, int b) {
return a + b;
}Define return type before method name. Use return keyword to send value back.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Forgetting return statement in non-void methodWhy it happens: If method returns int or String, every code branch must end in a return statement.
How to fix: Ensure all execution paths return a value matching the declared type.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Create a square number method
Complete the static method "square" that takes int n and returns n * n.
In main, call square(6) and print "Result: " + square(6).
Finished reading and practicing?
Mark this lesson as completed to update your course progress.