Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How can you Create a inner class inside a method

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 253
    Comment on it

    In java can we create a inner class inside a method??

    Answer to this query is yes .

    Example code :

     class Outer
        {
            String outerVar="outer Variable";
            public void checkInner()
            {
                final String localVar="Local Variable";
    
                class Inner
                {
                    public void check()
                    {
                        Outer o2=new Outer();
                        System.out.println("outer var ::"+outerVar);
                        System.out.println("local var ::"+localVar);
                    }
                }
    
                Inner i1=new Inner();
                i1.check();
            }
        }
     public class NestedClass {
            public static void main(String args[])
            {
    
                Outer o1=new Outer();
                o1.checkInner();
    
            }
    
        }
    

    In the above code Outer class is the enclosing class and Inner class is the non static nested class which is defined inside a checkInner() method of the Outer class.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: