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

后端 未结 17 1619
梦谈多话
梦谈多话 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:51

    • There is no runtime impact, as compiler automatically replaces the * with concrete class names. If you decompile the .class file, you would never see import ...*.

    • C# always uses * (implicitly) as you can only using package name. You can never specify the class name at all. Java introduces the feature after c#. (Java is so tricky in many aspects but it's beyond this topic).

    • In Intellij Idea when you do "organize imports", it automatically replaces multiple imports of the same package with *. This is a mandantory feature as you can not turn it off (though you can increase the threshold).

    • The case listed by the accepted reply is not valid. Without * you still got the same issue. You need specify the pakcage name in your code no matter you use * or not.

提交回复
热议问题