Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java access static nested class [duplicate]

How can i create a instance of the following Class and access its methods. Example:

public class A {
    public static class B {
        public static class C {
            public static class D {
                public static class E {
                    public void methodA() {}
                    public void methodB(){}
                }
            }
        }
    }
}
like image 323
Leonardo Avatar asked Jul 16 '26 15:07

Leonardo


1 Answers

You can use :

A.B.C.D.E e = new A.B.C.D.E();//create an instance of class E
e.methodA();//call methodA 
e.methodB();//call methodB

Or like @Andreas mention in comment you can use import A.B.C.D.E;, so if your class is in another packager then you can call your class using name_of_package.A.B.C.D.E like this:

import com.test.A.B.C.D.E;
//     ^^^^^^^^------------------------name of package

public class Test {

    public static void main(String[] args) {
        E e = new E();
        e.methodA();
        e.methodB();
    }
}
like image 70
YCF_L Avatar answered Jul 18 '26 03:07

YCF_L