Advertisement
//Java - Example of method local inner class
class OuterC
{
public void outerMethod()
{
class InnerC //Inner class cannot have a public/private modifier as it's local to a method
{
public void innerMethod() //but it's method can have any modifier because it's declared inside a class.
{
System.out.println("Inner method");
}
}
System.out.println("Outer method");
}
public static void main(String... ar)
{
OuterC ob= new OuterC();
ob.outerMethod();
}
}
Outer method
Advertisement
//Java - Example of method local inner class
public class OuterC
{
int b=20;
public void outerMethod()
{
int a=10;
//method-local innner class
class InnerC
{
public void innerMethod()
{
System.out.println("Inner Method");
System.out.println("Accessing outerMethod() member variable, a = " + a);
System.out.println("Accessing outer class member variable, b = " + b);
}
}
InnerC ob = new InnerC(); //method-local inner class can only be instantiated right after its definition ends in the method.
ob.innerMethod(); //calling inner class's method
}
public static void main(String... ar)
{
OuterC ob= new OuterC();
ob.outerMethod();
}
}
Inner Method
Accessing outerMethod() member variable, a = 10
Accessing outer class member variable, b = 20
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement