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

后端 未结 17 1613
梦谈多话
梦谈多话 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.

    0 讨论(0)
  • 2020-11-21 13:51

    Importing all the classes in a package is considered a blind approach. A major reason for this is that it clutters the class namespace and could lead to conflicts between classes in different packages with the same name.

    Specifically populating the necessary classes avoids that problem and clearly shows which versions were wanted. This is good for code maintainability.

    0 讨论(0)
  • 2020-11-21 13:53

    Here are the few things that I found regarding this topic.

    • During compilation, the compiler tries to find classes that are used in the code from the .* import and the corresponding byte code will be generated by selecting the used classes from .* import. So the byte code of using .* import or .class names import will be same and the runtime performance will also be the same because of the same byte code.

    • In each compilation, the compiler has to scan all the classes of .* package to match the classes that are actually used in the code. So, code with .* import takes more time during the compilation process as compared to using .class name imports.

    • Using .* import helps to make code more cleaner

    • Using .* import can create ambiguity when we use two classes of the same name from two different packages. Eg, Date is available in both packages.

        import java.util.*;
        import java.sql.*;
      
        public class DateDemo {
            private Date utilDate;
            private Date sqlDate;
        }
      
    0 讨论(0)
  • 2020-11-21 13:54

    In a previous project I found that changing from *-imports to specific imports reduced compilation time by half (from about 10 minutes to about 5 minutes). The *-import makes the compiler search each of the packages listed for a class matching the one you used. While this time can be small, it adds up for large projects.

    A side affect of the *-import was that developers would copy and paste common import lines rather than think about what they needed.

    0 讨论(0)
  • 2020-11-21 13:56

    In DDD book

    In whatever development technology the implementation will be based on, look for ways of minimizing the work of refactoring MODULES . In Java, there is no escape from importing into individual classes, but you can at least import entire packages at a time, reflecting the intention that packages are highly cohesive units while simultaneously reducing the effort of changing package names.

    And if it clutters local namespace its not your fault - blame the size of the package.

    0 讨论(0)
  • 2020-11-21 13:56

    Forget about cluttered namespaces... And consider the poor soul who has to read and understand your code on GitHub, in vi, Notepad++, or some other non-IDE text editor.

    That person has to painstakingly look up every token that comes from one of the wildcards against all the classes and references in each wildcarded scope... just to figure out what in the heck is going on.

    If you're writing code for the compiler only - and you know what you're doing - I'm sure there's no problem with wildcards.

    But if other people - including future you - want to quickly make sense of a particular code file on one reading, then explicit references help a lot.

    0 讨论(0)
提交回复
热议问题