Cases when Java import is not required (unusual qualification) [Question changed]

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

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

In this case, we qualify the Inner class with the Outer class, so we need to import the Outer class:

import mypackage.Outer;

Are there any other similar cases? (That is, when an unusual qualification arises - by an unusual one I mean not :) fullQualifier.identifier.

I exclude the case of automatic import (java.lang, primitive types, etc.)

+3
source share
3 answers

I think you misunderstood the construct you described:

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

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

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

Alternatively, you can write this:

import path.to.Outer;
import path.to.Outer.Inner;

// ...

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

, , :

  • . :

    java.util.Date d = new java.util.Date();
    
  • java.lang, . String
+1

the external package in this case included the internal package, so there was no need to import the internal package, in most cases there is no need to import the whole package only for using one component. For example, I only want to use String, there is no need to import the whole java.lang. When using some complex libraries, if you use some IDEs, they can fix the import for you, for example, in netbeans ctrl + shift + I will fix your import.

-1
source

Source: https://habr.com/ru/post/1785726/


All Articles