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

后端 未结 17 1969
梦谈多话
梦谈多话 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: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;
        }
      

提交回复
热议问题