LearnFrenzy
Sign in Take a free test

Methods · Lesson 17 of 42

Defining and Calling Methods

5 min read Updated Nov 7, 2026 Saurabh Samir

A method is a named block of code that does one job: calculate tax, validate an email, send a notification. Methods let you write logic once and reuse it, give complex code readable names, and test each piece on its own. In professional Java code, most methods are only a few lines long.

In this lesson

  • The parts of a method: modifiers, return type, name, parameters
  • Returning values and void methods
  • How a call works: the call stack
  • Designing good methods

Anatomy of a method

public class Main {
    // modifiers  return type  name      parameters
    public static double     withGst(double price, double rate) {
        return price + price * rate;      // send a value back
    }

    public static void main(String[] args) {
        double total = withGst(1000, 0.18);   // call it, use the result
        System.out.println(total);
        System.out.println(withGst(499, 0.05));
    }
}

Output

1180.0
523.95
PartMeaning
public staticModifiers. For now, static means you call the method on the class without creating an object (chapter 8 explains instance methods).
doubleReturn type – the type of value sent back. void means nothing is returned.
withGstName, in camelCase, usually a verb.
(double price, double rate)Parameters – inputs with types. The values passed in a call are arguments.

The method name plus its parameter types form its signature – withGst(double, double). The return type is not part of the signature.

void methods and early return

public class Main {
    static void printReceipt(String item, int qty, double price) {
        if (qty <= 0) {
            System.out.println("Nothing to print");
            return;                       // leave a void method early
        }
        System.out.printf("%-8s x%d  ₹%.2f%n", item, qty, qty * price);
    }

    public static void main(String[] args) {
        printReceipt("Pen", 3, 10);
        printReceipt("Bag", 0, 899);
    }
}

Output

Pen      x3  ₹30.00
Nothing to print

Common mistake: "missing return statement"

A non-void method must return a value on every path. static int sign(int n) { if (n > 0) return 1; else if (n < 0) return -1; } fails to compile because nothing is returned when n is 0. Add a final return 0;.

What happens during a call

Each call gets its own stack frame holding its parameters and local variables. When the method returns, the frame is removed and execution continues where the call was made.

public class Main {
    static int square(int n) {
        return n * n;
    }

    static int sumOfSquares(int a, int b) {
        return square(a) + square(b);
    }

    public static void main(String[] args) {
        System.out.println(sumOfSquares(3, 4));
    }
}

Output

25

The call stack at its deepest point

  1. square(n = 3)top frame – running now, returns 9
  2. sumOfSquares(a = 3, b = 4)waiting for square(3), then square(4)
  3. main(args)waiting for sumOfSquares

When an exception is thrown, Java prints this stack as the stack trace – read from the top to see where the error happened and how the program got there.

Methods calling methods: building a small program

public class Main {
    static double average(int[] marks) {
        int sum = 0;
        for (int m : marks) sum += m;
        return (double) sum / marks.length;
    }

    static String grade(double avg) {
        if (avg >= 85) return "A";
        if (avg >= 70) return "B";
        return "C";
    }

    static String report(String name, int[] marks) {
        double avg = average(marks);
        return String.format("%s: average %.1f, grade %s", name, avg, grade(avg));
    }

    public static void main(String[] args) {
        System.out.println(report("Nikhil", new int[]{88, 92, 79}));
        System.out.println(report("Pooja", new int[]{65, 71, 70}));
    }
}

Output

Nikhil: average 86.3, grade A
Pooja: average 68.7, grade C

Designing good methods

  • One job each. If you need "and" to describe it, split it.
  • Verb names that say what it does: calculateEmi, isValidPan, sendOtp. Boolean methods often start with is, has or can.
  • Return results rather than printing, so callers can use or test them.
  • Few parameters. More than three or four usually means they belong together in an object.
  • Short. If a method doesn't fit on your screen, it is probably doing too much.

Practice

Try it yourself

  1. Write isLeapYear(int year) and test it on 2024, 1900 and 2000.

    Show solution
    public class Main {
        static boolean isLeapYear(int y) {
            return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
        }
    
        public static void main(String[] args) {
            for (int y : new int[]{2024, 1900, 2000}) System.out.println(y + " " + isLeapYear(y));
        }
    }

    Output

    2024 true
    1900 false
    2000 true
  2. Write emi(principal, annualRatePercent, months) using EMI = P·r·(1+r)ⁿ / ((1+r)ⁿ − 1).

    Show solution
    public class Main {
        static double emi(double p, double annualRate, int months) {
            double r = annualRate / 12 / 100;
            double f = Math.pow(1 + r, months);
            return p * r * f / (f - 1);
        }
    
        public static void main(String[] args) {
            System.out.printf("₹%.2f%n", emi(500000, 9, 60));
        }
    }

    Output

    ₹10379.18

Quick check

Is the return type part of a method's signature?

No. The signature is the name plus parameter types.

What does void mean?

The method returns no value.

What is stored in a stack frame?

The method's parameters, local variables and the point to return to.

Summary

  • A method has modifiers, a return type, a name and parameters; name + parameter types = signature.
  • Non-void methods must return a value on every path; return also exits early.
  • Each call gets a stack frame; keep methods small, named with verbs, and returning results.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links