3

Is "localhost" a constant anywhere in Java SE 6? It's not terribly difficult to type out, but it might be nice to have a constant in many places rather than the String. Are there standard practices around this?

Edit: I know how to create a constant. In the past, I've found constants such as org.apache.http.HttpHeaders cleaner than using my own, as they are less prone to typos or accidental edits.

mbarrows
  • 555
  • 5
  • 15
  • 3
    I'm not sure I understand the question. Are you asking how to make a constant? Did you create a constant for this string and get an error somewhere? – David Jan 19 '13 at 03:24
  • public static String LOCAL_HOST = "localhost" or public static String LOCAL_HOST = "127.0.0.1".... – Jayamohan Jan 19 '13 at 03:25
  • 1
    There is not a constant provided by the Java. – jco.owens Jan 19 '13 at 03:29
  • @Jayamohan its better to use final – HRgiger Jan 19 '13 at 03:33
  • @HRgiger.. Yes true. Missed it. Cant edit my comment now. – Jayamohan Jan 19 '13 at 03:34
  • 3
    I completely don't understand why this was closed. This is a useful question---in fact, the same question I had, which is how I found this question. The less things hard-coded in the code, the better---and the less room for error. This is in the same vein as http://stackoverflow.com/q/13498275/421049, which was also immensely useful. Using system constants is almost always better. – Garret Wilson Oct 10 '14 at 16:04
  • 1
    It should have been closed as a request for an offsite resource, namely the L index of the Javadoc. 'Not constructive' is itself not constructive. – user207421 Oct 11 '14 at 00:43
  • I think it's a fine question. – Andrew Swan Oct 28 '14 at 22:45

2 Answers2

6

Well, you can get the local host's name like this:

InetAddress.getLocalHost().getHostName()

The previous snippet will return the local host's configured name (not really the string "localhost"). I believe you're better off writing your own constant:

public static final String LOCAL_HOST = "localhost";

And (although some people will say it's a bad practice, but that's open to debate) you can import the constant statically in any class where you need it, just replace "package.to" with the real package where the class resides and TheClass with the actual name of the class defining the constant:

import static package.to.TheClass.LOCAL_HOST;

In this way, using the constant will be a simple matter of writing LOCAL_HOST where needed.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    The problem is that there *may be* NO registered name for the loopback IP address. If you use `getLocalHost().getHostName()` it may be `null` which is liable to lead to NPEs if you don't allow for it. If you hard-wire "localhost" it may fail to resolve. If you put either of these behind a constant, then you get the illusion of something that "should always work" ... – Stephen C Oct 11 '14 at 00:21
2

Is "localhost" a constant anywhere in Java SE 6?

The simple answer is No.

You can confirm this by going to the Java SE javadoc index page for "L" and searching. There is no public "localhost" or local_host" constant listed in the javadoc in any capitalization.

It's not terribly difficult to type out, but it might be nice to have a constant in many places rather than the String.

First, I disagree that it would be "nice". IMO, it would make code less readable. SomeInterface.LOCAL_HOST is more characters than "localhost". And if you used a static import, the reader still has to lookup the definition of LOCALHOST to figure out what it has actually be bound to.

Second, and more importantly, the "localhost" name is actually a convention rather than a standard. Old Windows systems (for example) didn't provide a default mapping for the "localhost" name, so there is no guarantee that the name would resolve. (And even if it does, there is no guarantee that it will resolve to a loopback IP address.) So if the Java SE APIs did define a symbol for the "localhost" hostname, it would (in theory) raise portability issues.

And note that "localhost" and InetAddress.getLocalHost().getHostName() are most likely not the same. The former typically resolves to 127.0.0.1 and the latter typically resolves to an IP address that other systems can route packets to; i.e. NOT a 127.x.x.x IP address. Does it matter? In a lot of contexts, yes! For example, it is common practice to a use 127.x.x.x addresses when you explicitly do not want the IP traffic to leave the current host; e.g. because it is being sent unencrypted.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I agree it would make code less readable, especially because half the developers would use Interface.LOCAL_HOST, and the other half would just use the String "localhost" as they do today. – Philip Tenn Jan 19 '13 at 04:44
  • So following this logic it would seem that these commentators would find any constant definition undesirable. I completely disagree. – Garret Wilson Oct 10 '14 at 16:06
  • 2
    @GarretWilson - You are miscontruing my argument as a debating tactic. If >>I<< follow the logic of my argument, I don't arrive at that conclusion. For what it is worth, I also think the position that "all constant definition is undesirable" is unsupportable. But it is your strawman ... not mine. – Stephen C Oct 11 '14 at 00:37
  • @StephenC, what you had against a LOCALHOST constant variable is 1) that it uses more characters than the literal value, 2) that you don't know the literal value unless you look up the defining class. (The other arguments were against the actual value.) Both arguments 1) and 2) apply to all constant variable definitions that are made in a separate class. You will likely keep argument with me, but at the end of the day some poor person (mbarrows) just wanted to know if there was a constant definition so that his code could be consistent and utilize the standard library, and (s)he got beat down. – Garret Wilson Oct 15 '14 at 00:21
  • @GarretWilson - What I really have against a LOCALHOST constant is that it is **impossible** to give it a value that is always going to be correct. The programmer needs to be aware that if they hard-wire "localhost" into their code, directly or via a constant, there code will not work on all machines. That is the real point. As far as I am concerned, the debate about whether constants *per se* are "good" or "bad" is a complete red herring. You brought it up, not me. – Stephen C Oct 15 '14 at 02:18
  • @GarretWilson - As far as I am concerned, there no "debate" going on here. You are asserting that I think something and am saying something. I am telling you that I am not. End of story. – Stephen C Oct 15 '14 at 02:26