Java · Basics

Methods

Methods group code into named, reusable actions. Good method names make programs read like plain English.

Beginner 5 min Lesson 7 of 31Examples target Java 21 LTS (they also run on Java 25).
Before you start:Loops
0/31 in Java
On this page · 5 min · Beginner

In simple words#

A method is a vending machine button: you give inputs (coins and a choice), it does its work inside, and gives you something back (a snack).

Parameter
Input declared by the method.
Return type
Type of the value given back (void = nothing).
Overloading
Same name, different parameters.

Learning objectives#

  • Declare methods with parameters and return values
  • Call static methods
  • Overload methods
  • Understand pass-by-value

Explanation#

Syntax: modifiers returnType name(parameters) { body }. void means no return value.

static methods belong to the class (Math.max); instance methods belong to objects.

Overloading: several methods with the same name but different parameter lists. Varargs (int... values) accept any number of arguments.

Java is always pass-by-value: primitives are copied; for objects, the reference is copied (so the method can change the object’s contents, but not which object the caller’s variable points to).

Code example: Reusable price helpers#

JavaReusable price helpers Illustrative
public class Pricing {
    static double withGst(double amount) {
        return amount * 1.18;
    }


    static double withGst(double amount, double rate) {   // overload
        return amount * (1 + rate);
    }


    static int sum(int... values) {                       // varargs
        int total = 0;
        for (int v : values) total += v;
        return total;
    }


    public static void main(String[] args) {
        System.out.println(withGst(1000));
        System.out.println(withGst(1000, 0.05));
        System.out.println(sum(499, 1299, 250));
    }
}
Expected output
1180.0
1050.0
2048

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. withGst returns a calculated double.
  2. The overload accepts a custom rate.
  3. sum accepts any number of ints.
  4. main calls each method.

Real-world use case#

Pricing, tax and discount rules live in small methods that every part of a billing app reuses.

Common mistakes#

Methods doing too much

Split long methods; each should do one thing.

Expecting a method to reassign the caller’s variable

Java passes copies; return the new value instead.

Best practices#

  • Verb names: calculateTotal, sendInvoice
  • Few parameters
  • Return values rather than printing inside
  • Keep methods short

Remember this#

returnType name(params) { }

Overloading = same name, different params.

Java is always pass-by-value.

Practice exercise#

Your task: Write a method isAdult(int age) that returns true for 18 or above.

Show solution
Java Illustrative
static boolean isAdult(int age) {
    return age >= 18;
}

Mini quiz#

Methods quizMultiple choice Question 1 of 1

What does void mean as a return type?

Score so far: 0/0

Want more? Practise all Java MCQs · Java interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.