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
281k62 gold badges718 silver badges744 bronze badges
asked Jun 10, 2017 at 12:55
Leonardo
4711 gold badge4 silver badges7 bronze badges
1 Answer 1
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();
}
}
answered Jun 10, 2017 at 12:57
Youcef LAIDANI
60.3k21 gold badges112 silver badges178 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Andreas
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.Youcef LAIDANI
yes @Andreas this is also can solve the problem check my edit
Andreas
Arrsyss? If that was supposed to be example for package, then it should be lowercase, and a better example might be org.example.Youcef LAIDANI
no no @Andreas my mistake check now
lang-java
A.B.C.D.E e = new A.B.C.D.E();not sure what "How" means.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 declaredstatic.