前言
最近在分析httpclient(v4.2)源码的时候,发现了一个比较有意思的事情,那就是关于java中如何定义常量的问题。我想在Java中定义常量并使用常量是非常非常常见的,那么如此常见的问题,又有什么好探讨的呢?但即便是这样常见的一个问题,如果仔细的去分析并加以总结的话,也会觉得非常的有趣。
我们先来看一下httpclient中是如何定义并使用常量的,以下为截取的部分代码片段:
/**
* Parameter names for HTTP client parameters.
*
* @since 4.0
*/
public interface ClientPNames {
...
/**
* Defines whether redirects should be handled automatically
*
* This parameter expects a value of type {@link Boolean}.
*
*/
public static final String HANDLE_REDIRECTS = "http.protocol.handle-redirects";
/**
* Defines whether relative redirects should be rejected. HTTP specification
* requires the location value be an absolute URI.
*
* This parameter expects a value of type {@link Boolean}.
*
*/
public static final String REJECT_RELATIVE_REDIRECT = "http.protocol.reject-relative-redirect";
/**
* Defines the virtual host to be used in the Host