开发者

Order of import statements in java [closed]

开发者 https://www.devze.com 2023-04-12 04:43 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. 开发者_C百科 Want to improve this question? Update the question so it can be answered with facts and citations
Closed. This question is opinion-based. It is not currently accepting answers.
开发者_C百科

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 8 years ago.

Improve this question

Just to know. Which is the proper way of ordering import statements? Also which has more readability?

like,

  • External classes (like java.util.List) and then internal package

    classes.

  • Just in alphabetical order

Thanks in advance.


From the Java Programming Style Guidelines

The import statements must follow the package statement. import statements should be sorted with the most fundamental packages first, and grouped with associated packages together and one blank line between groups.

..... .....

The import statement location is enforced by the Java language. The sorting makes it simple to browse the list when there are many imports, and it makes it easy to determine the dependiencies of the present package The grouping reduce complexity by collapsing related information into a common unit.

Refere the Java Tutorial link for more info.


Most preferred, and used in most IDE, is alphabetical ordering, starting from domain level and a fully qualified class name.

java.* and javax.* takes precedence, and the rest are ordered.

Example:

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

import com.neurologic.http.HttpClient;
import com.neurologic.http.impl.ApacheHttpClient;


Im not sure if there is a standard. But some projects such like android use the following rule.

  1. First import project specific files (android)
  2. Second comes the third party files and library.
  3. The java standard api files.

Each group is seperated by an extra line. And each group has their imports in alphabetical order.

AFAIK these are based on our preference.


I just use the default order that my IDE (Eclipse) implements ... and regularly run the "Tidy Imports" thingy to keep the house in order.

Readability is not a significant concern if you automate this. You will quickly get used to any automated ordering, no matter what it is. Besides, people tend not to read imports anyway.


I prefer alphabetical order - this is the most readable, isn't it...


As others have mentioned, if you're using an IDE like Eclipse or IntelliJ, readability isn't too much of a concern because you start to trust that the organization is automated and perfect.

The one area, then, where order does matter, is defining the precedence if you have multiple classes with the same name potentially imported via a .* notation.

For example, say you have java.util.List and org.northpole.christmas.List available and you specify imports java.util.* and org.northpole.christmas.* Then in this case it makes sense to have java.util.* higher than org.northpole.christmas.* because if I wasn't really paying that much attention and I was reading the code later, I would assume that List is java.util.List and not something else. This is why, I believe, Eclipse has java and javax first, then org.apache, then others. These days I also slip com.google in above or below org.apache.


I prefer this :

1) First group imports based on external and internal APIs 2)Alphabetically within each group


Most of the IDEs does this Job nicely. Just right click and say "Organize Imports".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号