Order of import statements in java [closed]

2019-03-12 21:49发布

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.

8条回答
看我几分像从前
2楼-- · 2019-03-12 21:58

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

查看更多
对你真心纯属浪费
3楼-- · 2019-03-12 22:02

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

查看更多
We Are One
4楼-- · 2019-03-12 22:10

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.

查看更多
贼婆χ
5楼-- · 2019-03-12 22:11

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.

查看更多
干净又极端
6楼-- · 2019-03-12 22:12

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.

查看更多
再贱就再见
7楼-- · 2019-03-12 22:14

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;
查看更多
登录 后发表回答