1

In some of the resources I have seen online about Java they say that it is not good to use the * in an import statement in Java. For example import java.io.*

I was wondering why this is considered bad practice. Is is solely because it makes it hard for another programmer to know exactly what classes you are using under the java.io package or is there another reason for it?

user279185
  • 123
  • 1
  • 1
  • 6

1 Answers1

-3

Because Java.io.* imports the entire IO class. This means the program will be loading components that it won't need.

Mike
  • 13
  • 4
  • Nothing is loaded unnecessarily. A class will be loaded when it's used, and importing doesn't count as usage. – Kayaman Mar 02 '15 at 14:41
  • 1
    It's all about ambiguity. Both `javax.swing` and `java.util` have a `Timer` class. If you imported both of those packages using wildcards then tried using `Timer`, your program wont know which one to use – Vince Mar 02 '15 at 14:43
  • 1
    "*Because Java.io.* imports the entire IO class.*", first of all, not class but package. Second thing is that it will not import entire package (whatever you mean by that). You need to know that at compilation process simple names of classes (like `String`, `InputStream`) need to be replaced with full-qualified-name (like `java.lang.String`, `java.io.InputStream`) so at runtime JVM would know which classes need to be loaded. `import` provides list of possible places to look for such full names. – Pshemo Mar 02 '15 at 14:52