Java Methods Notes

Student-Friendly Notes for High School Computer Science

What is a method?

A method in Java is a named block of code that performs a specific task.

A method lets you group instructions together so they can be used whenever needed.

Think of a method like:

Example

public static void sayHello() {
    System.out.println("Hello!");
}

This method is named sayHello. When it is called, it prints Hello!.

Why do we use methods?

Methods are important because they help programmers write better code.

Real-life example

Without methods, you might have to rewrite the same code again and again.

With methods:

This makes programs cleaner and easier to manage.

Basic method syntax

public static void methodName() {
    // code to run
}

Parts of a method

public static void sayHello() {
    System.out.println("Hello!");
}

Calling a method

Defining a method does not make it run. You must call the method.

public class MethodExample {
    public static void sayHello() {
        System.out.println("Hello!");
    }

    public static void main(String[] args) {
        sayHello();
    }
}

Output

Hello!

Methods with parameters

A parameter is information given to a method.

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

Calling it:

greetUser("Joe");
greetUser("Maria");

Output

Hello, Joe
Hello, Maria

Parameters make methods more flexible. One method can work for many different inputs.

Methods that return a value

Some methods do a calculation and send a value back.

public static int addNumbers(int a, int b) {
    return a + b;
}

Calling it:

int sum = addNumbers(4, 6);
System.out.println(sum);

Output

10

Important vocabulary:

void methods vs return methods

void method

A void method does something, but does not send a value back.

public static void printMessage() {
    System.out.println("Welcome!");
}

return method

A return method sends a value back.

public static int multiply(int x, int y) {
    return x * y;
}

Example with multiple methods

public class MethodsDemo {
    public static void welcome() {
        System.out.println("Welcome to the program.");
    }

    public static int squareNumber(int num) {
        return num * num;
    }

    public static void main(String[] args) {
        welcome();
        int answer = squareNumber(5);
        System.out.println("The square is " + answer);
    }
}

Output

Welcome to the program.
The square is 25

Benefits of methods

Good method names

Method names should clearly describe what the method does.

Good examples:

Poor examples:

Common mistakes students make

1. Defining a method but never calling it

public static void hello() {
    System.out.println("Hi");
}

This will not run unless you call:

hello();

2. Forgetting parentheses when calling a method

Wrong:

hello;

Correct:

hello();

3. Forgetting the return statement in a return method

Wrong:

public static int add(int a, int b) {
    a + b;
}

Correct:

public static int add(int a, int b) {
    return a + b;
}

4. Using the wrong return type

If a method says it returns int, it must return an integer.

Summary

A method is a block of code that performs a task.

We use methods because they:

Methods can:

Quick Review Questions

  1. What is a method in Java?
  2. Why are methods useful?
  3. What is the difference between a void method and a return method?
  4. What is a parameter?
  5. What does the return keyword do?