yawnmoth said:
Is there a particular reason why Java doesn't automatically do "import
java.*" for all programs? It can be specified manually, but I'm just
wondering why Java doesn't already do it. Indeed, there are quite a
few Java apps that don't do this and, instead, import classes one
import statement at a time.
As was mentioned earlier, |import java.*| doesn't actually do anything.
But let's assume you meant something more like |import java.util.*|.
So why are so-called star imports frowned upon? Several reasons.
First, name collisions. If I import all of java.util and java.awt, what
do I mean when I refer to List, the collections interface or the toolkit
widget? Such an import, in short, ruins one of the primary reasons for
namespaces: to avoid name collisions.
Another reason is that it makes code harder to read. If I'm not quite
sure where ObscureClassName is held, and I go up and see |import
org.acme.foobar.*| and |import org.evilgenius.foobaz.*| I'm none the
wiser. But if I see |import org.acme.foobar.ObscureClassName|, I can
instantly tell in which package it is.
It's also a little more aesthetically pleasing, especially if you're
only importing one or two classes. If you have to import many classes,
that's when the star imports become useful. But if you're getting to
that point, it may be indicating something about your package structure.