Search code examples
javaimportjls

Cases where the imports in Java aren't needed (unusual qualification) [Question Edited]


I've noticed there are some special ways to qualify an entity in Java:

Object o = new Outer().new Inner();

In this case, we are qualifying the Inner class with the Outer class, so we only need to import the Outer class:

import mypackage.Outer;

Are there any other cases like this? (That is, where an unusual qualification occurs - by unusual I mean not: fullQualifier.identifier).

I'm excluding the case of the automatic imports (java.lang, primitive types, etc.)


Solution

  • I think you misunderstand the construct you've described:

    Object o = new Outer().new Inner();
    

    is actually a way to fully qualify the Inner class' constructor, just as in

    Outer.Inner i = new Outer().new Inner();
    

    On the other hand, you could write this:

    import path.to.Outer;
    import path.to.Outer.Inner;
    
    // ...
    
    Inner i = new Outer().new Inner();