Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Local Inner Classes

It is possible to define inner classes inside a method or even within the loop.

Example

class OuterClass 
{ 
    public void outerClassMethod() 
    {
        class InnerClass
        {
            public void innerClassMethod() 
            {
                System.out.println("Here I am!");
            }
        }
		
        InnerClass iObj = new InnerClass();
        iObj.innerClassMethod();
    }
}

class LocalInnerClassDemo 
{
    public static void main(String args[])
    {
        OuterClass outObj = new OuterClass();
        outObj.outerClassMethod();
    }
}

Output

Here I am!

 

Leave a reply