Java Tutorial: Create and Call Reusable Methods
This tutorial will guide you through creating and calling reusable methods in Java. Methods are blocks of code that perform specific tasks, and they’re fundamental to writing modular and maintainable Java programs. Reusable methods are methods designed to be called multiple times from different parts of your code, avoiding repetition and making your program more efficient.
Example 1: A Simple Addition Method
Let’s start with a very simple method that adds two numbers together. We’ll define a class called `MathUtils` and create a method called `add` within it.
public class MathUtils {
// This is a public method named 'add'
public static int add(int a, int b) {
int sum = a + b;
return sum;
}
}
Explanation:
- We create a class named `MathUtils` to hold our utility methods.
- The `add` method is declared as `public static int add(int a, int b)`.
- `public`: Means the method can be accessed from anywhere.
- `static`: Means the method belongs to the class itself, not to any specific object of the class.
- `int`: Specifies that the method returns an integer value.
- `add`: The name of the method.
- `int a, int b`: The method accepts two integer arguments, `a` and `b`.
- Inside the method, we calculate the sum of `a` and `b` and store it in a variable called `sum`.
- Finally, we return the value of `sum`.
Now, let’s call this method from the `main` method:
public class Main {
public static void main(String[] args) {
int result = MathUtils.add(5, 3);
System.out.println("The sum is: " + result);
}
}
Explanation:
- We create a class named `Main` to hold our main execution logic.
- Inside the `main` method, we call the `add` method from the `MathUtils` class using the dot notation: `MathUtils.add(5, 3)`.
- We store the returned value in a variable called `result`.
- We print the value of `result` to the console.
The sum is: 8
Example 2: A Method to Calculate the Square of a Number
Let’s create a method that calculates the square of a given number. This demonstrates a slightly more complex calculation.
public class MathUtils {
public static int square(int number) {
int result = number number;
return result;
}
}
Explanation:
- This method `square` takes an integer `number` as input.
- It calculates the square by multiplying the number by itself.
- It returns the calculated square.
Calling the method in the `main` method:
public class Main {
public static void main(String[] args) {
int num = 4;
int squareResult = MathUtils.square(num);
System.out.println("The square of " + num + " is: " + squareResult);
}
}
The square of 4 is: 16
Example 3: A Method to Calculate the Area of a Rectangle
This example builds on previous concepts. Let’s create a method to calculate the area of a rectangle.
public class GeometryUtils {
public static double rectangleArea(double length, double width) {
double area = length width;
return area;
}
}
Explanation:
- The `rectangleArea` method accepts the length and width of a rectangle as `double` values (to handle decimal values).
- It calculates the area by multiplying the length and width.
- It returns the calculated area.
Calling the method in the `main` method:
public class Main {
public static void main(String[] args) {
double length = 5.5;
double width = 10.0;
double area = GeometryUtils.rectangleArea(length, width);
System.out.println("The area of the rectangle is: " + area);
}
}
The area of the rectangle is: 55.0
Common Mistakes and Corrections:
- Incorrect Method Signature: Forgetting the `static` keyword can cause compilation errors, especially when working with classes that might have instances.
- Incorrect Return Type: Using the wrong return type (e.g., `String` when an `int` is expected) can lead to type mismatch errors.
- Scope Issues: Making variables within the method `final` if you intend to modify them within the method (this is generally not a requirement for simple methods, but good practice to know).



Leave a Reply