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(){}
                }
            }
        }
    }
}
3
  • A.B.C.D.E e = new A.B.C.D.E(); not sure what "How" means. Commented Jun 10, 2017 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: An inner class is a nested class that is not explicitly or implicitly declared static. Commented Jun 10, 2017 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. Commented Jun 10, 2017 at 15:34

1 Answer 1

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();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

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.
yes @Andreas this is also can solve the problem check my edit
Arrsyss? If that was supposed to be example for package, then it should be lowercase, and a better example might be org.example.
no no @Andreas my mistake check now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.