In java, the methods and variables which we create in a class can only be called by using the object of that class or, in case of static methods, we can directly call it by using the name of the class. The methods and variables can be called with the help of the dot operator. But there is a special case that a method can also be called by another method with the help of class name, but the condition is they should be present in the same class.
Syntax:
class Main { method1(){ // statements } method2() { // statements // calling method1() from method2() method1(); } method3() { // statements // calling of method2() from method3() method2(); } }
Example 1:
In the following example, the class contains two methods namely Area() and Volume() which is calculating the area and volume of a sphere. The main( ) method is calling the area() method to calculate the area of a sphere and the volume( ) method to calculate the volume of a sphere and then displays the result.
Java
// Java Program implementing the Nesting of Methods public class NestingMethodCalls1 { public void Area( double r) { // value of r is coming from main method from which // this method is called Math.pi is used for getting // the value of pi System.out.println( "****** Inside Area method ******" ); // calculate area double a = 4 * Math.PI * r * r; // print the area System.out.println( "Surface area of a Sphere is : " + a); } public void Volume( double r) { // value of r is coming from main method from which // this method is called Math.pi is used for getting // the value of pi System.out.println( "****** Inside Volume method ******" ); // calculate the volume double v = ( 4 / 3 ) * Math.PI * r * r * r; // print the volume System.out.println( "Volume of a Sphere is : " + v); } public static void main(String args[]) { // creating object of class Lazyroar Lazyroar gfg = new Lazyroar(); // calling of method Area() from main method gfg.Area( 12 ); // calling of method Volume() from main method gfg.Volume( 12 ); } } |
****** Inside Area method ****** Surface area of a Sphere is : 1809.5573684677208 ****** Inside Volume method ****** Volume of a Sphere is : 5428.672105403162
Example 2:
In the following example, the class contains two methods namely swap() and gfg1(). The swap() method is used to swap two variables and the gfg1() method is used for adding two numbers. The main() method is calling the gfg1() method with two values to add two numbers and the gfg1() method is calling swap() method for performing swapping of two numbers.
Java
// Java Program implementing the Nesting of Methods public class NestingMethodCalls2 { public void swap( int x, int y) { System.out.println( "*********This is swap method***********" ); System.out.println( "Before swapping:x=" + x + " " + "y=" + y); // swap the numbers int z = x; x = y; y = z; System.out.println( "After Swapping:a=" + x + " " + "b=" + y); } public void gfg1( int a, int b) { System.out.println( "*********This is gfg1 method***********" ); System.out.println( "Before performing operation:a=" + a + " " + "b=" + b); a = a + 10 ; b = b + 12 ; System.out.println( "After operation:a=" + a + " " + "b=" + b); // calling of swap() method from gfg1() method swap(a, b); } public static void main(String args[]) { // creating the object of class Lazyroar2 Lazyroar2 gfg2 = new Lazyroar2(); int a = 20 , b = 30 ; // calling of method gfg1() from main method gfg2.gfg1(a, b); } } |
*********This is gfg1 method*********** Before performing operation:a=20 b=30 After operation:a=30 b=42 *********This is swap method*********** Before swapping:x=30 y=42 After Swapping:a=42 b=30
Example 3:
In the following example, the class contains three methods namely a1(), a2(), a3(). The main() method is calling a3() method with values a and b and after that, the a2() is calling another method a1() then this method performs all his operations and then pass their values to another method means this method then call another method namely a2() then this will execute and then the program gets terminated.
Java
// Java Program implementing the Nesting of Methods public class NestingMethodCalls3 { public void a1( int a, int b) { a = a + 10 ; b = b + 20 ; System.out.println( "****** Inside a1 method ******" ); System.out.println( "a = " + a + " " + "b = " + b); // calling method a2() from a1() with parameters a // and b a2(a, b); } public void a2( int x, int y) { x = x + 100 ; y = y + 200 ; System.out.println( "****** Inside a2 method ******" ); System.out.println( "x = " + x + " " + "y = " + y); } public void a3( int w, int z) { w = w + 50 ; z = z - 50 ; System.out.println( "****** Inside a3 method ******" ); System.out.println( "w = " + w + " " + "z = " + z); // calling method a1() from method a3() with // parameters w and z a1(w, z); } public static void main(String[] args) { // creating the object of class Lazyroar3() Lazyroar3 gfg3 = new Lazyroar3(); int a = 100 , b = 200 ; // calling method a3() from main() method gfg3.a3(a, b); } } |
****** Inside a3 method ****** w = 150 z = 150 ****** Inside a1 method ****** a = 160 b = 170 ****** Inside a2 method ****** x = 260 y = 370