Great Deals

How To Create Nested Class in java

19:41 suresh chaudhari 0 Comments

InnerClass
class A {
            class B {
                        public void print() {
                                    System.out.println("HI ");
                        }
            }


}

public class InnerClasses {

            public static void main(String[] args) {
                        A aObj=new A();
                       
                        A.B bObj=aObj.new B();
                       
                                    bObj.print();           

            }

}

 Inner Class2

class AOuter {
            class BInner {
                        public void printMe() {
                                    System.out.println("i m B");
                        }

            }
           
            public void aMethod()
            {
                        BInner b=new BInner();
                        b.printMe();
            }
}

public class MethodLocalInnerClass {

            public static void main(String[] args) {
                        AOuter a=new AOuter();
                                    a.aMethod();

            }

}
Method Local Inner Class


class AOuter {
            void aOuter()
            {
            class BInner {
                        public void printMe() {
                                    System.out.println("i m B");
                        }

            }
            BInner bObj=new BInner();
            bObj.printMe();
            }
           
}

public class MethodLocalInnerClass {

            public static void main(String[] args) {
                        AOuter a=new AOuter();
                        a.aOuter();

            }

}


Static Nested Class

class AOuter {
           
            static class BInner {
                        public void printMe() {
                                    System.out.println("i m B");
                        }

            }          
}
public class MethodLocalInnerClass {

            public static void main(String[] args) {
                        AOuter.BInner a=new AOuter.BInner();
                        a.printMe();
            }
}

0 comments:

Advertising