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 开发者_StackOverflow社区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.)
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();
Also, you wouldn't need to import a class if:
you use the full path to the object. For example:
java.util.Date d = new java.util.Date();
the class is in the same package
- the class is in the
java.lang
package e.g.String
the outer package in this case included the inner package, thats why there was no need ti import the inner package, in most cases there is no need to import a whole package just to use one component ..for example i only want to use a String, there is no need ti import the whole java.lang. In some complicated libraries usage if you are using some IDEs they can fix the imports for you, for example in netbeans ctrl+shift+i will fix your imports
精彩评论