Stop 03 · Control Flow, Methods & ArraysTopics · 5/8

Defining Methods

Part of the Java Roadmap

In short

Copy-pasting the same ten lines in five places is how bugs multiply. Methods let you write a piece of logic once, give it a name, and call it wherever you need it.

Methods group reusable logic with a return type, a name and parameters. Static methods belong to the class and can be called without creating an object.

The jaadu presses a big round button on a shiny chai machine as a steaming paper cup slides out of the bottom.

Think of it like this

The chai vending machine

You press a button, put in milk and sugar choices, and out comes a cup of chai. You do not need to know how the machine boils the water. A method is that machine: inputs go in as parameters, the work happens inside, and one result comes out as the return value.

Watch it happen

Calling gst(1000, 18)

main
gst()

gst(1000, 18)

1/4main calls the method and passes two arguments.

The big ideas

What you need to know

The signature

static double gst(double amount, int rate): modifiers, return type, name and parameters.

return

Sends a value back and ends the method. A void method returns nothing and can use a bare return; to exit early.

Local scope

Variables declared inside a method exist only there. Each call gets fresh copies.

static for now

static methods belong to the class and can be called from main directly. Instance methods come with objects in the next phase.

See it in code

Walk through the code

java
1public class Shop {2 3    static double gst(double amount, int ratePercent) {4        return amount * ratePercent / 100.0;5    }6 7    static boolean isEligibleForCod(double total, String pincode) {8        if (total > 5000) return false;9        return pincode.length() == 6;10    }11 12    static void printLine(String label, double value) {13        System.out.printf("%-10s ₹%.2f%n", label, value);14    }15 16    public static void main(String[] args) {17        double price = 1000;18        double tax = gst(price, 18);19        printLine("Price", price);20        printLine("GST", tax);21        printLine("Total", price + tax);22        System.out.println("COD allowed: " + isEligibleForCod(price + tax, "560001"));23    }24}

1/4Takes two inputs and returns a double. 100.0 avoids integer division.

Three small methods: one calculates, one decides and one prints. main reads like a short story.

Save yourself the pain

Common mistakes

Oops

A non-void method where one path has no return.

Do this instead

The compiler says "missing return statement". Every path must return a value.

Oops

One giant main with 200 lines.

Do this instead

Split it into small methods that each do one job and have a clear name.

Your mission

Build a mini toolkit

~20 min

Quick check

What does void mean in static void greet()?

Go deeper

  1. 1Move repeated code from earlier programs into static methods.
  2. 2Write methods that return values instead of printing them.
  3. 3Name methods with verbs in camelCase, like calculateTotal().

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge