Java Methods

Methods is one of the most important concepts to understand in Object Oriented Language. Think of methods as mini-programs inside your program—they help you break your code into organized, reusable chunks.

In this blog post, we’ll cover:

  • What a method is
  • Why methods are useful
  • How to define and call methods
  • Method parameters and return values
  • Method overloading
  • A few best practices

What Is a Method in Java?

A method is a block of code that performs a specific task. You define it once and can use it (or “call” it) as many times as needed.

Here’s a basic method:

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

This method doesn’t return anything (void) and doesn’t take any parameters. It simply prints a message.


Why Use Methods?

Reusability

Write once, use many times.

Organization

Break complex programs into smaller, manageable pieces.

Readability

Methods make your code easier to understand and maintain.


How to Define a Method in Java

modifier returnType methodName(parameterList) {
    // method body
}
Java

The general syntax for a method:

Example:

public static int add(int a, int b) {
    return a + b;
}
Java
  • public: Access modifier
  • static: Indicates the method belongs to the class, not instances
  • int: Return type
  • add: Method name
  • (int a, int b): Parameters
  • return a + b;: Method body

How to Call a Method

Calling a method means telling Java to execute the code inside that method.

int result = add(5, 3);
System.out.println(result);  // Output: 8
Java

If the method returns a value, you can store it in a variable or use it directly.


Method Parameters

Parameters are like inputs to a method.

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

Calling it:

greetUser("Alice");  // Output: Hello, Alice!
Java

Returning Values

Methods can return values of any data type.

public static double square(double num) {
    return num * num;
}
Java

Calling:

double sq = square(4.5);  // sq is 20.25
Java

Method Overloading

You can define multiple methods with the same name but different parameter lists.

public static int multiply(int a, int b) {
    return a * b;
}

public static double multiply(double a, double b) {
    return a * b;
}
Java

Java knows which one to use based on the argument types.


Real-World Example: Calculator

public class Calculator {
    public static int add(int x, int y) {
        return x + y;
    }

    public static int subtract(int x, int y) {
        return x - y;
    }

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

    public static double divide(double x, double y) {
        if (y == 0) {
            System.out.println("Cannot divide by zero!");
            return 0;
        }
        return x / y;
    }

    public static void main(String[] args) {
        System.out.println("Add: " + add(4, 2));
        System.out.println("Subtract: " + subtract(4, 2));
        System.out.println("Multiply: " + multiply(4, 2));
        System.out.println("Divide: " + divide(4.0, 2.0));
    }
}
Java

Best Practices for Java Methods

  • Keep methods small and focused on one task
  • Use meaningful names (calculateTotal, getUserInput)
  • Avoid too many parameters (if you need many, consider creating a class)
  • Use return values wisely—don’t always rely on void
  • Comment your methods if their purpose isn’t obvious

Conclusion

Methods are essential to Java programming. They make your code cleaner, more organized, and easier to debug or extend. Once you understand how to write and use methods, you’re well on your way to mastering the basics of Java.


Challenge for You

Write a method that checks whether a number is prime. Bonus: Try method overloading for checking primes with int and long types.