Static and Non Static Inner class example


/**
 *
 * @author bhavesh
 */

public class OuterClass {
 
    private static int ox;
    private int oy;
    public class MyInner1
    {
        private int x;
     
        public void test()
        {
            System.out.println(new OuterClass.MyInner2().y);
         
            System.out.println(oy);
        }
    }
 
    public class MyInner2 {
        private int y;
     
    }
 
    public static class Nested1 {
        private int z;
     
        public void test2() {
           //return Nested2.w;
            System.out.println(Nested2.w);
            System.out.println(ox);
         
        }
    }
 
    public static class Nested2 {
     
        static private int w;
     
     
    }
 
}



// Application Class

public class MyOuterApplication {
   
   
    public static void main(String[] args) {
       
        OuterClass o1 = new OuterClass();
       
        OuterClass.MyInner1 i1 = o1.new MyInner1();
       
        OuterClass.Nested2 n1 = new OuterClass.Nested2();
       
       
       
       
    }
   
}


Comments

Popular posts from this blog

Custom Exception example for BankAccount

Annotation

Interface with class and interface as member