1

I am a student, and a couple of the books I have been reading (Java for Dummies, for one) has said using the wildcard import statement is bad programming practice and encourage the reader to avoid using it. Whereas, in class, we are encouraged to use it. Can somebody please explain why it is poor programming practice?

If so, what adverse affects does it have on the program performance? For example, slow it down.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user2810581
  • 41
  • 1
  • 2
  • 4
  • 3
    That's a purely syntactic construct; it has no effect at runtime. – SLaks Oct 01 '13 at 14:17
  • @SLaks, when the runtime engine is linking all the code, won't it link more code rather than the code that you NEED? Therefore, decreasing startup-time? – Rohan Oct 01 '13 at 14:19
  • 1
    The compiler picks and chooses the specific libraries that are actually needed, I believe. It's still problematic to import via wildcard though. See the answer below. – nhgrif Oct 01 '13 at 14:20
  • http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad – Charu Khurana Oct 01 '13 at 14:22
  • @RoK: No. Java does not have anything like a linker. The class-loader loads each class when it's first _used_. – SLaks Oct 01 '13 at 14:51

1 Answers1

3

The more you insert, the higher the change that you will get a naming collision where two classes have the same class name:

http://en.wikipedia.org/wiki/Name_collision

The first example i can find within the java API are: http://docs.oracle.com/javase/6/docs/api/javax/naming/Binding.html http://docs.oracle.com/javase/6/docs/api/org/omg/CosNaming/Binding.html

Enigma
  • 1,699
  • 10
  • 14
  • 2
    Specifically, `java.util.Date` and `java.sql.Date` are a really annoying example of this issue. – Mureinik Oct 01 '13 at 14:20
  • Which is typically not that big of a worry, but... with Java and Java for Android are slightly different, and it's important that you're making the right, specific import. There are some overlaps between standard Java and Java for Android, I believe. – nhgrif Oct 01 '13 at 14:20
  • Do not forget the case where the user specifies a class name which is already present in the java API – Enigma Oct 01 '13 at 14:22