Java System setProperty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 2+

Definition

In Java, System.setProperty(key, value) stores value under key in the JVM's system properties and returns the value that was there before, or null if the key was new.

The change is visible to every later getProperty call in the same running program, including calls inside libraries. It is the code version of java -Dkey=value. It only lasts until the JVM stops; nothing is saved, and other programs never see it.

A static method of the java.lang.System class, added in Java 2 (JDK 1.2). Call it on the class name, as in System.setProperty(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass a key and a value; keep the return value if you want to restore it later:

Java
String old = System.setProperty("app.mode", "test");  // previous value or null
System.getProperty("app.mode");                         // "test"
CallWhat it does
System.setProperty("app.mode", "test")Adds or replaces the property; returns the old value.
System.setProperty("app.mode", old)Puts a saved value back (only if old is not null).
System.clearProperty("app.mode")Removes it instead; the way to "set" a property back to missing.
System.setProperty("app.mode", null)Throws NullPointerException; values cannot be null.

Added in Java 1.2. Set properties before the code that reads them runs; code that already read and stored a value will not notice the change.

Library declarations
Java
// java.lang.System
public static String setProperty(String key, String value)

Parameters

ParameterDescription
keyProperty name. Must not be null or empty.
valueNew value. Must not be null.

Return Value

Return ValueDescription
StringThe previous value of the property, or null if it had none.

Compare with Similar Methods

Ways to change system properties:

MethodHow it differs
java -Dkey=valueSets the property before main starts; the usual way to configure.
clearProperty(key)Removes a property and returns its old value.
setProperties(p)Replaces the whole set of properties at once.
getProperties().put(k, v)Also works, but skips the key check and returns an Object; prefer setProperty.

Java Examples

Example 1 — setProperty Returns the Previous Value

Set the same key twice and look at what each call returns.

Java
public class SystemSetPropertyReturn {
    public static void main(String[] args) {
        String first = System.setProperty("app.theme", "light");
        String second = System.setProperty("app.theme", "dark");

        System.out.println("First call returned: " + first);
        System.out.println("Second call returned: " + second);
        System.out.println("Current value: " + System.getProperty("app.theme"));
    }
}

How It Works

  1. A brand-new key. "app.theme" did not exist, so the first setProperty creates it and returns null, meaning "there was no old value".
  2. Replacing hands back the old value. The second call overwrites "light" with "dark" and returns "light", the value it replaced.
  3. The last write wins. getProperty("app.theme") now returns "dark" everywhere in this JVM.

Example 2 — Change How a Method Behaves Without Changing Its Code

greet reads a property each time it runs, with "Hello" as the default. Setting the property changes the next greeting.

Java
public class SystemSetPropertyGreeter {
    static String greet(String name) {
        String word = System.getProperty("greeter.word", "Hello");
        return word + ", " + name + "!";
    }

    public static void main(String[] args) {
        System.out.println(greet("Ana"));
        System.setProperty("greeter.word", "Welcome");
        System.out.println(greet("Ana"));
        System.setProperty("greeter.word", "Hi");
        System.out.println(greet("Ben"));
    }
}

How It Works

  1. Default first. Before any setProperty call, "greeter.word" is missing, so greet falls back to "Hello".
  2. Visible immediately. After setProperty("greeter.word", "Welcome"), the very next call to greet reads "Welcome", because it looks the property up each time.
  3. Read late, not early. If greet had stored the word in a static field when the class loaded, later changes would be ignored, which is how many JDK settings behave.

Example 3 — Temporarily Override a Property and Restore It

Tests often change a property for one step and must leave everything as it was. Because values cannot be null, a missing old value is restored with clearProperty.

Java
public class SystemSetPropertyRestore {
    public static void main(String[] args) {
        String old = System.setProperty("report.format", "csv");
        try {
            System.out.println("During the test: " + System.getProperty("report.format"));
        } finally {
            if (old == null) {
                System.clearProperty("report.format");
            } else {
                System.setProperty("report.format", old);
            }
        }
        System.out.println("After restoring: " + System.getProperty("report.format"));

        try {
            System.setProperty("report.format", null);
        } catch (NullPointerException e) {
            System.out.println("null value: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Save while setting. The return value of setProperty is the old value (here null), so no separate getProperty call is needed.
  2. Restore in finally. The finally block runs even if the test code throws. Since old is null, it removes the key, and the property reads as null again.
  3. null is not a value. setProperty(key, null) throws NullPointerException, which is exactly why the restore code needs the clearProperty branch.

Frequently Asked Questions

The previous value of the property, or null if the property did not exist before.
No. setProperty throws NullPointerException for a null value. Use System.clearProperty(key) to remove the property.
No. System properties live only inside the running JVM. Use a file, a database, or java.util.prefs to keep settings.
Both create system properties. -D sets them before main starts; setProperty sets them while the program runs, so only code that reads the property afterwards sees the new value.
The JDK reads many standard properties once at startup and caches them, so changing them later may have no effect.

Next: setSecurityManager()

Install a SecurityManager. Throws UnsupportedOperationException on Java 18+ by default.

setSecurityManager() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful