Why is using a wild card with a Java import statement bad?

后端 未结 17 1616
梦谈多话
梦谈多话 2020-11-21 13:07

It is much more convenient and cleaner to use a single statement like

import java.awt.*;

than to import a bunch of individual classes

17条回答
  •  一向
    一向 (楼主)
    2020-11-21 13:58

    The most important one is that importing java.awt.* can make your program incompatible with a future Java version:

    Suppose that you have a class named "ABC", you're using JDK 8 and you import java.util.*. Now, suppose that Java 9 comes out, and it has a new class in package java.util that by coincidence also happens to be called "ABC". Your program now will not compile on Java 9, because the compiler doesn't know if with the name "ABC" you mean your own class or the new class in java.awt.

    You won't have that problem when you import only those classes explicitly from java.awt that you actually use.

    Resources:

    Java Imports

提交回复
热议问题