Search code examples
javaandroidstaticinner-classesnon-static

Instantiation of inner class of another class


I have the following setup which I would think would work but is giving an error in my IDE (Android Studio):

// MyClass1.java
public class MyClass1{
    public MyClass1(){}
    public class MyNestedClass1{}
}

// MyClass2.java
public class MyClass2{
    public static MyClass1 MY_CLASS1 = new MyClass1();
    public MyClass2(){
        new MY_CLASS1.MyNestedClass1(); //Error
    }
}

The specific IDE error is:

cannot resolve symbol MyNestedClass1


Solution

  • The notation is

    MY_CLASS1.new MyNestedClass1(); //No Error
    

    Syntax is

    <Expression that resolves to a reference of the enclosing class>.new [Name of nested inner class](..)