What is the proper style for listing imports in Java?

前端 未结 10 1028
梦谈多话
梦谈多话 2021-02-18 16:01

Is it better to list each individual piece of a package you\'re going to need (see #1) or is it better to just import everything from a package (see #2)?

1



        
10条回答
  •  情书的邮戳
    2021-02-18 16:14

    The latter is generally considered "bad form". With tool support, there's no excuse for not listing them individually, and it removes any ambiguity.

    Mind you, Eclipse by default uses wildcards for static imports, and explicit imports for normal ones. Not sure why it makes that distinction.

    edit: the distinction is obvious in hindsight - static imports often refer to static methods, and you can't unambiguously refer to a static method by name (due to overloading). So if it can't be fully unambiguous, you may as well use a wildcard.

提交回复
热议问题