I have a logical question: Why i cannot import all packages from all packages in java? For example i can import all classes from java.awt:
import java.awt.*;
But the following isnt possible:
import java.awt.*.*;
My aim would be to import all stuff from awt.image and awt.event and so on. Is there another way to do this?
Thank you!
There is no way to achieve an
import a.package.*.*;
in Java. The JLS, Section 7.5 specifies the only 4 types of imports that are legal:e.g.
import java.util.List;
e.g.
import java.awt.*;
e.g.
import static org.junit.Assert.assertEquals;
e.g.
import static org.junit.Assert.*;
Packages allow classes of the same name to be referenced individually. E.g. there is
java.awt.List
andjava.util.List
. What would stop someone from importing everything withjava.*.*;
. How wouldList
be resolved then? There would be too much ambiguity.No, and using wildcard imports is bad style in general as it makes your code harder to read.
Some disadvantages of using wildcards imports:
Edit: Seems like importing more classes than required doesn't result in any bulky code, but I would still prefer to import the classes explicitly to have a clear idea about what I am working with.