About Us Contact Us Write for Us Advertise
Home > Java > Java Methods Explained: Reusable Code with Parameters and Return Values
Java

Java Methods Explained: Reusable Code with Parameters and Return Values

Learn Java methods the simple way — how to write and call methods, pass parameters, return values, and organise your code the way professionals do.

Shiv Pandey
Shiv Pandey
Sep 19, 2026 | 1 views
Java Methods Explained: Reusable Code with Parameters and Return Values

As your programs grow, you'll notice you keep writing the same few lines over and over. Methods fix that. A method is a named block of code that does one job — you write it once and then "call" it whenever you need it. If you've heard the word function in other languages, a method is Java's version of that. This lesson makes them simple.

Why methods matter

Methods give you three big wins:

  • No repetition — write the logic once, reuse it everywhere (the "DRY" principle: Don't Repeat Yourself).
  • Readability — a well-named method like calculateTax() tells you what's happening at a glance.
  • Easier fixes — a bug lives in one place, so you fix it once.

You've actually been using a method all along: main is a method, and System.out.println() is a method you call.

Writing your first method

Here's a simple method that greets someone, and the main that calls it:

public class Main {

    // our own method
    static void greet() {
        System.out.println("Hello there!");
    }

    public static void main(String[] args) {
        greet();   // call it
        greet();   // call it again — reuse!
    }
}

This prints "Hello there!" twice. We defined the logic once and called it as many times as we liked. (Don't worry about static for now — while we're in main, just include it; we'll explain it properly when we get to objects.)

Parameters: giving a method input

Methods become powerful when you pass them parameters — values they work with. Put them in the parentheses, each with a type and a name:

static void greet(String name) {
    System.out.println("Hello, " + name + "!");
}

// calling it:
greet("Asha");   // Hello, Asha!
greet("Ravi");   // Hello, Ravi!

The value you pass in ("Asha") is called an argument; the variable that receives it (name) is the parameter. Same method, different results — that's the point.

Return values: getting an answer back

Often you want a method to calculate something and hand the result back. That's what a return type is for. Instead of void (which means "returns nothing"), you state the type of value it returns, and use the return keyword:

// takes two ints, returns their sum (an int)
static int add(int a, int b) {
    return a + b;
}

// using the returned value:
int total = add(5, 3);            // total is now 8
System.out.println(add(10, 20));  // prints 30

The method add promises to give back an int, so we can store its result in a variable or print it directly. This is exactly how System.out.println gets numbers to display.

Anatomy of a method

Putting it together, every method has this shape:

returnType methodName(parameters) {
    // body: the code that does the work
    return value;   // (only if returnType is not void)
}
Part Means
returnType Type of value given back (int, String…), or void for nothing
methodName A clear, verb-like name (calculateTotal)
parameters Inputs the method needs (can be none)

A practical example

static double areaOfCircle(double radius) {
    return 3.14159 * radius * radius;
}

// in main:
System.out.println(areaOfCircle(5));   // 78.53975

A tip on naming and size

Give methods clear, action-based names (sendEmail, isValidPassword) and keep each one focused on a single job. If a method is doing five things, that's usually a sign it should be split into smaller methods.

When I first learned methods, they were the moment my code stopped being one giant messy block and started feeling organised. Breaking a big problem into small, well-named methods is genuinely how professional code is written — you're learning a habit you'll use every single day as a developer.

Key takeaways

  • A method is a reusable, named block of code — write once, call many times.
  • Parameters are inputs; the values you pass are arguments.
  • The return type is what the method gives back; use void when it returns nothing.
  • Name methods clearly and keep each one focused on a single job.

← Previous: Lesson 8 — Loops in Java
Next: Lesson 10 — Arrays in Java →

Related Articles

Java Loops Explained: for, while and do-while (with Examples)
Java

Java Loops Explained: for, while and do-while (with Examples)

Java if, else and switch: Making Decisions in Your Code
Java

Java if, else and switch: Making Decisions in Your Code

Operators in Java Explained (with Examples)
Java

Operators in Java Explained (with Examples)

Java Syntax Basics: Your First Program Explained Line by Line
Java

Java Syntax Basics: Your First Program Explained Line by Line