3

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(){}
                }
            }
        }
    }
}
Sotirios Delimanolis
  • 263,859
  • 56
  • 671
  • 702
Leonardo
  • 371
  • 1
  • 3
  • 6
  • `A.B.C.D.E e = new A.B.C.D.E();` not sure what "How" means. – khachik Jun 10 '17 at 13:00
  • 3
    *FYI:* A class declared inside another class is called a *nested* class. If it is non-static, it is also called an *inner* class. Since your classes are `static`, they are generally called *static nested classes*. They are definitely not *inner* classes. JLS [§8.1.3. Inner Classes and Enclosing Instances](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.3): *An inner class is a nested class that is not explicitly or implicitly declared `static`.* – Andreas Jun 10 '17 at 13:18
  • OP, have you looked this up in the Java documentation? The JLS answers this question. FYI, the documentation for things, like, oh, the Java language for example, is very useful. – Lew Bloch Jun 10 '17 at 15:34

1 Answers1

3

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();
    }
}
YCF_L
  • 51,266
  • 13
  • 85
  • 129
  • or `import A.B.C.D.E; E e = new E();`. And you need to qualify first `E` too, without the import. --- And for completeness, you might mention how it works if `A` is in a package. – Andreas Jun 10 '17 at 12:57
  • yes @Andreas this is also can solve the problem check my edit – YCF_L Jun 10 '17 at 13:00
  • `Arrsyss`? If that was supposed to be example for package, then it should be lowercase, and a better example might be `org.example`. – Andreas Jun 10 '17 at 13:02
  • no no @Andreas my mistake check now – YCF_L Jun 10 '17 at 13:05