12 Java Fundamentals | Methods in Java | By Dummy for Dummies

Java Methods | Hassan Bukhari
Java Notes

12 Java Fundamentals | Methods in Java | By Dummy for Dummies

Java Beginner Guide Methods Functions Reusability

INTRODUCTION

What if I tell you, you can have an assistant in your program that will do a specific task assigned to it every time you ask. Every time you call the assistant, give it the data, it will do the assigned task. All you have to do is just code it for once, and then call it unlimited times to do your tasks.


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.

  • Boss Method → Always executes, code execution starts from here.
  • Assistant Methods → You can choose to execute them multiple times, or not even once; depends on you, executes when you call them.

Let's simulate a situation where the boss monkey will ask to see 2 bananas plus 3 bananas, how many total bananas? To make the assistant do the calculations, we need to send it the data. In programming, that data we send are called Arguments. As we know that we store data in variables, so when the assistant receives that data/arguments, it needs to store it in some variables, right? In programming, that variable which receives and stores arguments is called a Parameter. So we have:

  • Arguments → The data the boss monkey sends (2 bananas and 3 bananas)
  • Parameters → The variables that store arguments

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.

Boss Method = Orange   |   Assistant Method = Yellow

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) { }
public → It decides who can access this assistant method. When you reach OOP and start making bigger projects, you will create many Java files. If you write "public" at the start of such a method, then from any file you can access it. If you write "private", then only from that file where it belongs. As it decides who can access this method, it is called Access Modifier.
static → This concept is a bit more complex. Writing static means this part of code does not belong to each object you create from this, but rather all objects created from it will share this.
void → It tells the method to not return anything back. You can decide anything for it to return. When we added the two numbers, we printed it there. If we wanted, we could have returned the total value, for that we needed to write int instead of void. So void means empty, do not return anything.
assistant(int a, int b) → This is the method name. Since you can create multiple methods, each needs a name to see which one we are calling. After the name, we put brackets and inside them are parameters to receive the arguments in their respective variables. Parameters should be in the same order in which we sent the arguments.
{ } → This is where we write all that code which belongs to that specific method.

POINTS TO REMEMBER

⦿ While calling a method, write its name and arguments inside it. You do not need to write the datatype of the argument when calling from the main method, i.e., just assistant(3, 2).
⦿ When creating a specific method, for now just write public static, and decide the return type that suits you.
⦿ The order of arguments sent and parameters must match.
⦿ Method names should be in lower case alphabets (convention).
⦿ The number of arguments sent and received in parameters MUST be the same.
⦿ Methods do NOT send/receive variables; they just send/receive the data inside variables.

METHODS WITH SPECIFIC RETURN TYPE

Let's write a code which asks the user for a value, sends it as an argument, then the method returns another value, and we will print that value. To return a value, just write return at the end of the assistant method and the value you want to return in front of it. It will return that value to the exact place in the main method from where you called the assistant method. Here again, you need a variable to store it. So in that case, we first create a variable, write "=", and then call the method. Therefore, the returned value is directly assigned to the 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:

  • 1 → Addition
  • 2 → Subtraction
  • 3 → Multiplication
  • 4 → Division

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:

  • Use Scanner for input.
  • Create at least 4 separate methods: one for addition, one for subtraction, one for multiplication, one for division.
  • Use arguments, parameters, return values, and void methods.
  • Handle division carefully (if the second number is 0, print "Invalid: Division by Zero").

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.