class OuterC
{
private int a=10;
public void outerMethod()
{
InnerC innerOb = new InnerC();
innerOb.innerMethod();
}
public class InnerC
{
public void innerMethod()
{
System.out.println("Method of Inner Class is invoked");
System.out.println("Inner class accessing instance variable of Outer Class, a = "+ a);
}
}
public static void main(String... ar)
{
OuterC outerOb= new OuterC ();
outerOb.outerMethod();
}
}
Method of Outer Class
Method of Inner Class
Accessing member of Outer class from inner class, a = 10
public class OuterC
{
private int a=10;
public void outerMethod()
{
System.out.println("Method of Outer Class");
}
class InnerC
{
public void innerMethod()
{
System.out.println("Method of Inner Class");
System.out.println("Accessing member of Outer class from inner class, a = " + a);
}
}
public static void main(String... ar)
{
OuterC outerOb = new OuterC();
outerOb.outerMethod();
//ob.innerMethod(); // won't work
OuterClass.InnerClass innerOb = outerOb.new InnerClass(); //Creating an object of inner class.
innerOb.innerMethod(); //calling method of inner class
}
}
Method of Outer Class
Method of Inner Class
Accessing member of Outer class from inner class, a = 10
Coming Next
-
JSP & Servlets