Java Methods

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 19 / 50 · English

Java Methods

Give a reusable action a name.

Step-by-step explanationsRunnable JavaPractice + answer

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.
01 / The concept

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.

02 / Step by step

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 / கோடின் விளக்கம்

CodeMeaning
staticThe method belongs to the class, not to an object
intReturn type — what the method gives back. Use void if it returns nothing.
addMethod name — use camelCase by convention
(Opening parenthesis — parameters go inside
int aFirst parameter: type then name
int bSecond 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.

Java reference fragmentJAVA
// 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.0
03 / Read and predict

A 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.

Main.java · complete runnable exampleJAVA
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.

04 / Learn by changing

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.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

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.

05 / Your turn

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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.