Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Java have constants for well-known system property names? [closed]

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?

like image 321
Andrew Swan Avatar asked Apr 23 '09 00:04

Andrew Swan


2 Answers

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).

like image 58
Lachlan Avatar answered Sep 29 '22 12:09

Lachlan


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.

like image 43
lothar Avatar answered Sep 29 '22 12:09

lothar