Methods in Java

Subject: Data Structures and Algorithms · Language: Java · Level: beginner · 6 min read

Understand methods, parameters, arguments, return values, void, static methods, and reusable code.

A method is a block of code that performs a specific task. Instead of writing all logic inside `main`, we can create separate methods for separate work. Methods make a program easier to read, test, debug, and reuse. For example, if a program needs to calculate sum many times, writing one `add` method is better than repeating the same addition logic again and again.

In many other languages, methods are called functions. In Java, because code is written inside classes, we usually call them methods. For beginner DSA programs, methods help us keep logic clean and organized.

Method Syntax

```java

static int add(int a, int b) {

return a + b;

}

```

In this method, `static` allows the method to be called directly from the `main` method in beginner programs. The `int` before the method name tells Java that this method will return an integer value. `add` is the method name. A method name should clearly describe what the method does. The values inside parentheses, `int a` and `int b`, are parameters. They receive input values when the method is called.

The method body is written inside curly braces. Whatever task the method performs is written inside this body.

Parameters and Arguments

Parameters are variables written in the method definition. They act like placeholders that will receive values later. Arguments are the actual values passed when the method is called. In `add(10, 20)`, the values `10` and `20` are arguments. These values are copied into the parameters `a` and `b`.

This difference is important in interviews: parameters belong to the method definition, and arguments belong to the method call.

Method Overloading

Method overloading means creating more than one method with the same name but different parameter lists. Java decides which method to call by checking the number, type, or order of arguments.

```java

static int add(int a, int b) {

return a + b;

}

static double add(double a, double b) {

return a + b;

}

``` Both methods are named `add`, but one works with integers and the other works with decimal values. Overloading is useful when the action is conceptually the same, but the input type or number of inputs is different.

return Statement

`return` sends a value back to the place where the method was called. If a method returns `int`, it must return an integer value. If it returns `boolean`, it must return `true` or `false`.

```java

static int square(int n) {

return n * n;

}

```

When we call `square(5)`, the method calculates `5 * 5` and returns `25`. The returned value can be printed directly or stored in a variable.

void Method

A `void` method does not return a value. It only performs an action, such as printing a message or displaying output.

```java

static void printMessage() {

System.out.println("Welcome");

}

```

Use `void` when the method does not need to send a result back. Use a return type such as `int`, `long`, `boolean`, or `String` when the method should calculate and return a value.

Why static Is Used in Beginner Programs

In beginner Java programs, `main` is declared as `static`. A static method can directly call another static method in the same class. That is why many DSA examples use methods like `static int add(...)`. Later, when you study classes and objects deeply, you will also learn non-static methods.

Methods in DSA

Methods are very useful in DSA because they separate the main logic from helper logic. For example, a program may have separate methods to check prime number, reverse a number, find maximum, calculate sum, or search an array. This makes large solutions easier to manage. If a bug appears in one small method, it is easier to find and fix than searching through a very long `main` method.

Example Program

```java

import java.util.*;

public class Main {

static int add(int a, int b) {

return a + b;

} static int multiply(int a, int b) {

return a * b;

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

Scanner sc = new Scanner(System.in); System.out.print("Enter two numbers: ");

int x = sc.nextInt();

int y = sc.nextInt(); System.out.println("Sum = " + add(x, y));

System.out.println("Product = " + multiply(x, y));

}

}

```

This program has two methods: `add` and `multiply`. The `main` method reads input from the compiler input area, calls both methods, and prints the returned results. This keeps calculation logic separate from input-output logic.

Practice Question

Create methods to find the square of a number, cube of a number, and maximum of two numbers. Read values in `main`, call the methods, and print the returned answers.

More Java Lessons

Browse all PrepCampus study materials