Java Methods
Give a reusable action a name.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Methods.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
A method groups an operation behind a clear name. Separate its inputs, its returned value, and any side effects such as printing. A small method is easier to test when its result depends on explicit parameters.
Explore the details
Java Methods
A method is a reusable block of code that runs only when called. Define it once, call it anywhere — as many times as you need.
Method Anatomy
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
static | The method belongs to the class, not to an object |
int | Return type — what the method gives back. Use void if it returns nothing. |
add | Method name — use camelCase by convention |
( | Opening parenthesis — parameters go inside |
int a | First parameter: type then name |
int b | Second parameter |
) { | Opening brace — method body starts here |
} | Closing brace — method body ends here |
Defining and Calling
void means the method does not return anything — it just performs an action. When you need the method to produce a value, replace void with the return type (int, String, boolean, etc.) and add a return statement.
// Return a value
static int multiply(int a, int b) {
return a * b;
}
// Void — perform action, return nothing
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
System.out.println("4 × 7 = " + multiply(4, 7)); // 4 × 7 = 28
greet("Alice"); // Hello, Alice!
// Overloading — same name, different params
static double multiply(double a, double b) { return a * b; }
System.out.println(multiply(2.5, 4.0)); // 10.0A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
static int square(int n) { return n * n; }
public static void main(String[] args) { System.out.println(square(5)); }
}Predict first. Then reveal the expected output / வெளியீடு
25
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Call square with 0, -3, and 6.
Reveal the answer and reasoning / விடை
The results are 0, 9, and 36. The return value is computed from the supplied argument.
One mistake to avoid: A non-void method must return an appropriate value along every reachable path that completes normally.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.