Nesting of Methods in Java


A method can be called by using only its name by another method of the same class that is called Nesting of Methods. A method of a class can be called only by an object of that class using the dot operator. So, there is an exception to this. When a method in java calls another method in the same class, it is called Nesting of methods.

Source Code

//Nesting of Methods in Java
class demo {
    private int m, n;
 
    demo(int x, int y) {
        m = x;
        n = y;
    }
 
    int largest() {
        if (m > n)
            return m;
        else
            return n;
    }
 
    void display()
    {
        int large=largest();
        System.out.println("The Greatest Number is : "+large);
    }
 
}
public class nested_method {
    public static void main(String args[]) {
        demo o =new demo(10,20);
        o.display();
    }
 
}
 

Output

The Greatest Number is : 20
To download raw file Click Here

Basic Programs