12 Java Fundamentals | Methods in Java | By Dummy for Dummies
INTRODUCTION
What if I tell you, you can have an assistant in your program that will do a specific task assigned to it everytime you ask. Everytime 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 execute, code execution starts from here.
Assistant Methods → You can chose to execute them multiple times, or not even once depends on you, executes when you call them.
Let's simulate a situation where boss monkey will ask to see 2 bananas plus 3 bananas how much total bananas? To make 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 recieve that data/arguments it need to store it in some variables right? In programming that variable which receive and store arguments are called Parameter. So we have:
Arguments → The data Boss monkeys sends (2 bananas and 3 bananas)
Parameters → The Variable that stores Arguments
When we need the assistant, we will call it, tell it the data through the call, it will receive the data. What will the assistant do with data? we will code it to add two numbers.
Boss Method = Orange
Assistant Method = Yellow
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);
}
}
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 and when the method return value we will store it in 'result'
int result = squareNumber(number);
// Printing the returned value
System.out.println("Square of " + number + " is: " + result);
input.close(); //closes the scanner we created
}
// Assistant method: takes a number as parameter and returns its square
public static int squareNumber(int n) {
int a = n * n;
return a;
}
}
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.
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); // returns a value
int product = multiplyNumbers(num1, num2); // returns a value
// Calling a void method (just prints)
printResults(num1, num2, sum, product);
sc.close(); //closes the scanner we created
}
// 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);
}
}
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.
Example: addNumbers(int a, int b) returns the sum.
Example: printResult(int result) just prints (void).
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:
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