Write about NESTING of METHODS in Java?

NESTING OF METHODS :

A method of a class can be called only by an object of that class or [class itself in the case of static members] using the dot operator. However there is an exception to this a method can be called by using only its name by another method of the same class. This is known as nesting of methods.

In the following example the class nesting defines one constructor and two methods namely largest ( ) and display ( ). The method display ( ) calls the method largest ( )  to determine the largest of the two numbers and then displays the result.

Example:

class Nesting
{
int m, n;
Nesting (int x, int y)
{
m=x;
n=y;
}

int largest ( )
{
if (m >=n)
return(m);
else
return(n);
}

V
void display( )
{
int large=largest ( );
System.out. println(“largest value” large);
}
}

class Nestmain
{
public static void main ( String args[ ])
{
Nesting nest=new nesting(10, 20);
nest. Display( );
}
}

A method can call any number of methods. It is also possible for a called method to call another method, i. e method 1 may call method 2, which return may call method 3.

Related

Java 9187763020099676357

Post a Comment

emo-but-icon

item