🌱BeginnerMethods

Defining Methods

Write reusable blocks of code with Java methods - define, call, pass parameters, and return values.

Overview

A method is a named, reusable block of code that performs a specific task. Methods let you break programs into smaller, manageable pieces - instead of repeating the same logic, you write it once and call the method whenever you need it. Every Java method has an access modifier, a return type, a name, and optionally parameters. The return type specifies what value the method gives back (or void if it returns nothing). Parameters are the inputs the method needs to do its work.

Key Points

  • •Syntax: `accessModifier returnType methodName(parameters) { body }`
  • •void - the method does not return a value
  • •return value - the method produces a result; the return type must match
  • •Parameters are local variables that receive values when the method is called
  • •Arguments are the actual values passed when calling the method
  • •Java passes primitive arguments by value - the method gets a copy, not the original
  • •static methods belong to the class and can be called without creating an object

Examples

void method and method with return value

public class Main {
    // void - no return value
    static void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }

    // Returns an int
    static int add(int a, int b) {
        return a + b;
    }

    // Returns a double
    static double circleArea(double radius) {
        return Math.PI * radius * radius;
    }

    public static void main(String[] args) {
        greet("Alice");              // Hello, Alice!
        int sum = add(10, 25);
        System.out.println(sum);     // 35
        System.out.printf("Area: %.2f%n", circleArea(5.0)); // Area: 78.54
    }
}

static methods in the same class can be called directly. Non-static methods need an object instance.

Parameters are passed by value

public class Main {
    static void doubleIt(int x) {
        x = x * 2; // only changes the LOCAL copy
        System.out.println("Inside: " + x);
    }

    public static void main(String[] args) {
        int num = 10;
        doubleIt(num);
        System.out.println("Outside: " + num); // still 10!
    }
}

Primitives are copied when passed to a method. Changing the parameter inside the method does not affect the caller's variable.

Early return and multiple returns

public class Main {
    static String classify(int n) {
        if (n < 0) return "negative";
        if (n == 0) return "zero";
        if (n % 2 == 0) return "positive even";
        return "positive odd";
    }

    static int max(int a, int b, int c) {
        int biggest = a;
        if (b > biggest) biggest = b;
        if (c > biggest) biggest = c;
        return biggest;
    }

    public static void main(String[] args) {
        System.out.println(classify(-5));  // negative
        System.out.println(classify(0));   // zero
        System.out.println(classify(6));   // positive even
        System.out.println(max(3, 9, 5));  // 9
    }
}

A method can have multiple return statements. Execution ends at the first return encountered.

Common Mistakes

  • Forgetting the return statement in a non-void method - the compiler will flag "missing return statement"
  • Wrong return type: declaring a method as int but returning a String - compile error
  • Calling a non-static method without an object: `MyClass.nonStaticMethod()` will fail

Note

Keep each method focused on one thing (the single-responsibility principle). If you need more than 20 lines to explain what a method does, it probably should be split into two methods.

Exercises

Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.

Loading editor…

Completed: 0 / 4 exercises

Knowledge Check

Question 1 of 4

What does `void` as a return type mean?