views:

467

answers:

3

The java.lang.System class defines a number of well-known properties.

For example, you can obtain the JVM's temporary directory by looking up the "java.io.tmpdir" property:

... = System.getProperty("java.io.tmpdir");

What I don't understand is why these properties aren't defined as constants (e.g. in the java.lang.System class). This would be a lot less error-prone than using literal Strings. In other words, I would like to be able to do this:

... = System.getProperty(System.JAVA_IO_TMPDIR);

Any ideas why this wasn't done? It could even be added in a future release of Java without breaking backward compatibility. Or am I missing something obvious?

+4  A: 

My guess is that Sun did not want to commit to a predefined set of system properties. If they are not defined as contants they can add system properties at any time (even if they only release an incremental version of the JDK like from 1.4.1 to 1.4.2).

Edit:
Any predefined constants must be considered part of the API. Thus even changing the number of constants is an API change. By not defining any constants Sun is allowed to define new system properties without introducing an API change.

lothar
+1 Most definitely.
OscarRyz
I don't understand your reasoning here; using constants for the property names wouldn't stop new properties being added. In any case, it's way easier and less risky to add a constant than to implement the feature that the constant represents, so I don't know why they wouldn't do both at the same time. For example, it's a lot less work to add one line defining the System.JAVA_IO_TMPDIR constant than to implement the feature whereby a JVM can know its own temporary directory.
Andrew Swan
@Andrew: Because that way the source code of java.lang.System is totally decoupled from the property/key values the host system has.
OscarRyz
The system properties alway include a minimum set of keys, but is not limited to those. Different platforms may add new keys.
OscarRyz
@Andrew. Not coupling to the host but to the properties keys them selves. Think about this, java.lang.System was among the first classes in Java. Back then they didn't quite knew what was going to be the platform like. It was aimed for TV sets, Futurist devices ( such as *7 ) among other things. My guess ( because as S.Lott correctly points out only James Gosling and the original team will know now ) is they thought different platforms may return different system properties.
OscarRyz
A: 

What's the difference between a "literal" and a LITERAL?

Two characters: " and ".

Can't see why bother inventing a complex set of LITERALs when "literal" works just as well.

S.Lott
The difference is the compiler can tell you if there is a typo in LITERAL but not if there is a typo in "litteral".
jmucchiello
Plus, the IDE can autocomplete so you don't have to create a small program first to print the values and then copy/paste the result into the code.
OscarRyz
Because I'd much rather find errors at compile time than at runtime!
Andrew Swan
Agreed with all previous comments. "literal" does not work just as well, sorry.
unforgiven3
You may not like the reasoning, but it's what appears to be the case. It looks like the authors chose "Literal" over LITERAL. You may not like it, but that seems to be the decision that was made.
S.Lott
@S.Lott: Indeed, that's the decision but I doubt the reason was only : Hey "literal" does the job.
OscarRyz
I strongly suspect that it was a matter of - use an intern'd string, it does everything we need it to do without adding any additional language features like "enums" or even adding a bunch of static final int constants.
S.Lott
@Oscar Reyes: you have to ask Gosling that question, not Stack Overflow.
S.Lott
@S.Lott: You're right until then we'll never know. Probably he was writing the first version of oak.lang.System and James was coding the constants when someone came up and said: Drop that already, Why don't you just use "literal" instead? Come with me and have a cup of java :) :) :) hehehe. The rest is history.
OscarRyz
@S.Lott: My guess is that they thought this could be used from a *7 interface with no system properties, to TV devices and directly on web browsers. Probably they thought using a map ( dictionary ) would allow much more flexibility ( low coupling ) and system properties may vary. Later they realize standardizing was key if they want to succeed and those basic properties were fixed. But I agree with you, we don't know for sure. :)
OscarRyz
+2  A: 

All the properties documented under System.getProperties() are standardised - every Java SE implementation must provide them. There is no reason that Java 7 could not introduce constants for these standard property names. It would not prevent the introduction of new properties. I think it's just no one has thought it worth the effort (even trivial additions to core Java APIs have to go through processes I think).

Lachlan