Java System setProperty() Method
Example
Set a system property, change it, and print the values:
public class Main {
public static void main(String[] args) {
System.setProperty("app.color", "blue");
System.out.println(System.getProperty("app.color"));
String old = System.setProperty("app.color", "red");
System.out.println(old);
System.out.println(System.getProperty("app.color"));
}
}
Definition and Usage
The setProperty() method sets the value of a system property. If the property does not exist, it is created.
The change only lasts while the program runs.
Tip: Use the clearProperty() method to remove a system property.
Syntax
public static String setProperty(String key, String value)
Parameter Values
| Parameter | Description |
|---|---|
| key | Required. The name of the system property. |
| value | Required. The new value of the property. |
Technical Details
| Returns: | A String with the old value of the property, or null if it did not have a value. |
|---|
Related Pages
❮ System Methods