Where to get 'http' String literal in Java?

Short Answer :

There is no final string literal http constant in the Java SE JDK at the time of this post.

Longer Answer

I realize this is an old question but I don't think it has necessarily been answered adequately. I believe that Leon's answer is partially correct. Having a constant named HTTP with string value "http" is pointless in many cases. However there are situations where this is not pointless. Additionally this question illustrates the pervasive misunderstanding of what a protocol is and is not.

Constant, Protocol, Scheme

There are three main reasons to use final string literals in Java.

  • If you are going to use the same value in multiple places and want a single place to change the value if needed in the future.
  • To avoid the use of magic numbers or their String counterparts.

The first point is self evident but not likely in this case. The second point is applicable to the constant "http", but it depends on the usage, which I will briefly touch upon below as it relates to this example.

HTTP is an application protocol used for the transfer of hypermedia. It is one of many protocols defining how data is transferred over the world wide web (a term that is often confused with "internet"). The most recognizable aspect of the HTTP application protocol is the http: you see before a URI. However, that http: you see is not the protocol, it is the scheme. The scheme is a piece of the protocol, but is not the protocol. In that case it might be apt to have the following string literal constant.

final String SCHEME = "http";

Additionally, if you were writing some type of service client that works over multiple protocols, you might want to specify a default one. In that case the following final string literal would be appropriate.

final String DEFAULT_SCHEME = "http";

Does one exist?

Indeed the following text is supported by the fact that Apache does have an http constant that is used for the reason mentioned above.

org.apache.http.HttpHost.DEFAULT_SCHEME_NAME

This constant is also included in the older httpclient library when it was part of Apache Commons (it is now part of HttpComponent).

Recommendation

As of Java 9 there exists no Java SE constant for http. I would favor creating your own constant instead of using Apache's. I recommend this is because Apache's DEFAULT_SCHEME_NAME constant is specifically referring to the HttpHost class. Utilizing it for something else violates the single purpose principal of software engineering. To be more pragmatic, the HttpHost class could change their default scheme (maybe) without you changing your default scheme. This would obviously be an issue. However, Java EE does provide you with final string literals for all the HTTP methods, response statuses, header fields, and a myriad of other HTTP-related things such as authentication types, etc. I relate with your desire to have these string literals provided as constants in Javas standard library. It would just make me feel more whole.

Post Script

I want to point out another usage of a final string literals that was made by user @gorootde in a comment below.

The example

final String Z = "Z";

may still have value if this result is used multiple times throughout the code base in contrast to other "Z" that are of different significance. Without knowing the context I can't judge but this would be an odd choice that would require significant justification, however it's not outright pointless by definition.


There is this constant field http in the Apache library:

org.apache.http.HttpHost.DEFAULT_SCHEME_NAME

If you are using this library in your application, you can use it in your case.

Tags:

Java