← Back to all posts

12 Java Fundamentals

Methods in Java

By Dummy for Dummies


Introduction

What if you could have an assistant in your program, one that does a specific task every time you ask? You code it once, then call it as many times as you need, and it does its job every time.


Methods

In Java, a method is a block of code enclosed in curly brackets. There is a main method always, and you can create multiple other methods if you want. Think of these like you have one boss method and many assistants.

Let's simulate a situation: the boss monkey wants to know 2 bananas plus 3 bananas, how many total bananas? To do that math, the assistant needs the data sent to it. In programming, that data is called an Argument.

And since we store data in variables, the assistant needs a variable to catch that incoming data too. That variable is called a Parameter. So we have:

When we need the assistant, we will call it, tell it the data through the call, and it will receive the data. What will the assistant do with the data? We will code it to add two numbers.

Java
public class Main {
    // Boss method - program starts here
    public static void main(String[] args) {
        System.out.println("Boss Monkey: Send data to assistant!");
        // Boss calls the assistant to calculate bananas
        assistant(2, 3);
    }

    // Assistant method → Adds two numbers (bananas)
    public static void assistant(int a, int b) {
        int total = a + b;
        System.out.println("Total Bananas: " + total);
    }
}
Console Output
Boss Monkey: Send data to assistant! Total Bananas: 5

While creating the assistant method, you see there are a few words that need explanation. This is the assistant method:

public static void assistant(int a, int b) { }


Points to Remember


Methods with Specific Return Type

Let's write code that asks the user for a value, sends it as an argument, and gets a value back from the method. To return a value, write return at the end of the assistant method, followed by whatever you want to send back. That value lands right back at the exact spot in the main method where you called it.

You still need a variable to catch it though. So create the variable, write =, then call the method. The returned value gets assigned straight into that variable.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // Asking user for a number
        System.out.print("Enter a number: ");
        int number = input.nextInt();

        // Sending the number as an argument to the assistant method
        int result = squareNumber(number);

        // Printing the returned value
        System.out.println("Square of " + number + " is: " + result);
        input.close();
    }

    // Assistant method: takes a number as parameter and returns its square
    public static int squareNumber(int n) {
        int a = n * n;
        return a;
    }
}
Console Output
Enter a number: 7 Square of 7 is: 49

Exercise

Let's do an exercise for what we studied in this post. Study this code, break it down, analyze it and identify the concepts used here. It would be even better if you write down your observations and make a mini report on it.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Asking the user for two numbers
        System.out.print("Enter first number: ");
        int num1 = sc.nextInt();
        System.out.print("Enter second number: ");
        int num2 = sc.nextInt();

        // Calling assistant methods
        int sum = addNumbers(num1, num2);
        int product = multiplyNumbers(num1, num2);

        // Calling a void method (just prints)
        printResults(num1, num2, sum, product);
        sc.close();
    }

    // Assistant 1: adds two numbers and returns result
    public static int addNumbers(int a, int b) {
        return a + b;
    }

    // Assistant 2: multiplies two numbers and returns result
    public static int multiplyNumbers(int a, int b) {
        return a * b;
    }

    // Assistant 3: void → prints results, does not return anything
    public static void printResults(int x, int y, int sum, int product) {
        System.out.println("You entered: " + x + " and " + y);
        System.out.println("Their sum is: " + sum);
        System.out.println("Their product is: " + product);
    }
}
Console Output
Enter first number: 4 Enter second number: 6 You entered: 4 and 6 Their sum is: 10 Their product is: 24

Mini Project: Calculator with Methods

Your project is to create a simple Java calculator using methods. The program should ask the user to enter two numbers. Show a menu of operations:

The user chooses one operation. The program should call the correct assistant method to perform that operation. Print the result in a neat format using printf.

Rules:

Bonus (Optional): After showing the result, ask the user if they want to perform another calculation. If yes, repeat the process.


Example Output

Console Output
Enter first number: 12 Enter second number: 4 Choose operation: 1 → Addition 2 → Subtraction 3 → Multiplication 4 → Division Your choice: 3 Result: 12 * 4 = 48 Do you want to perform another calculation? (yes/no): no Goodbye!

Closing

That's it for methods in Java! Methods are like your program's assistants; they let you organize code into reusable, focused tasks. Once you understand how to pass data (arguments), receive it (parameters), and sometimes return results, your programs become cleaner, more powerful, and much easier to manage. You can re-use any method as much as you like. Code them once and use them multiple times.

← Previous 11 - Nested Loops in Java Next → 13 - Overloaded Methods in Java