Nesting of Methods in Java
In Java, the nesting of methods refers to the concept of defining a method inside another method. When a method is nested within another method, it is called a nested method or a local method. This is also known as method nesting or method local inner classes. Methods cannot be directly nested within other methods. Methods are defined within a class, and they exist at the class level. It is not possible to define a method directly inside another method. Instead, methods can be called or invoked from other methods. You can have a method call another method, creating a hierarchical structure of method calls. It can sometimes give the appearance of nesting methods, but they are not truly nested within each other.
Example 1:
Here in this program, we have taken input from the user. The prompt will be available for the user to enter the two numbers. Then the given program creates an object of the class and then calls a method for calculating the product of two numbers which was entered by the user.
FileName: ProductCalculator.java
import java.util.Scanner; public class ProductCalculator { // Method to calculate the product of two numbers public int multiply(int a, int b) { int product = nestedMultiply(a, b); // Call the nested method to calculate the product return product; // Return the product } // Nested method to perform the actual multiplication private int nestedMultiply(int x, int y) { return x * y; // Return the multiplication result } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object to read user input ProductCalculator calculator = new ProductCalculator(); // Create an instance of ProductCalculator class System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); // Read the first number from the user System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); // Read the second number from the user int result = calculator.multiply(num1, num2); // Calculate the product using the multiply() method System.out.println("The product of " + num1 + " and " + num2 + " is: " + result); // Print the result } }
Output:
Enter the first number: 8 Enter the second number: 9 The product of 8 and 9 is: 72
Example 2:
In the above program, we have taken 3 numbers from a user. The prompt will be available for the user to enter three numbers. Then use nested methods and built-in math.max function we found the largest number among the three numbers.
FileName: MaxFinder.java
import java.util.Scanner; public class MaxFinder { // Method to find the maximum of three numbers public int findMax(int a, int b, int c) { int max = nestedMax(a, b, c); // Call the nested method to find the maximum return max; // return max value } // Nested method to find the maximum among three numbers private int nestedMax(int x, int y, int z) { int tempMax = Math.max(x, y); // Find the maximum of x and y int max = Math.max(tempMax, z); // Find the maximum of tempMax and z return max; // return max value } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object to read user input MaxFinder finder = new MaxFinder(); // Create an instance of MaxFinder class System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); // Read the first number from the user System.out.print("Enter the second number: "); // Prompt the user to enter the second number int num2 = scanner.nextInt(); // Read the second number from the user System.out.print("Enter the third number: "); int num3 = scanner.nextInt(); // Read the third number from the user int result = finder.findMax(num1, num2, num3); // Calculate the maximum using the findMax() method System.out.println("The maximum of " + num1 + ", " + num2 + ", and " + num3 + " is: " + result); // Print the result } }
Output
Enter the first number: 23 Enter the second number: 11 Enter the third number: 24 The maximum of 23, 11, and 24 is: 24
Example 3
In the above program, we have taken the radius as input from the user then the program creates an object of the class and then calls the method for calculating the area of the circle.
FileName: CircleAreaCalculator.java
import java.util.Scanner; public class CircleAreaCalculator { // Method for calculate the area of a circle public double calculateArea(double radius) { double area = nestedCalculateArea(radius); // Call the nested method to calculate the area return area; // Return Area } // Nested method to calculate the area of a circle private double nestedCalculateArea(double radius) { double area = 3.14159 * radius * radius; // Calculate the area using the approximate formula A = 3.14159 * r^2 return area; // Return Area } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object to read user input CircleAreaCalculator calculator = new CircleAreaCalculator(); // Create an instance of CircleAreaCalculator class System.out.print("Enter the radius of the circle: "); // for entering the radius of circle double radius = scanner.nextDouble(); // Read the radius from the user double area = calculator.calculateArea(radius); // Calculate the area using the calculateArea() method System.out.println("The area of the circle with radius " + radius + " is: " + area); // Print the result } }
Output
Enter the radius of the circle: 7 The area of the circle with radius 7.0 is: 153.93791