Home » Java programming language

Java System class setProperty() method with example

System class setProperty() method: Here, we are going to learn about the setProperty() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 12, 2019

System class setProperty() method

  • setProperty() method is available in java.lang package.
  • setProperty() method is used to sets the system property denoted by the given parameter (system_property) with the given another parameter (system_property_value).
  • setProperty() method is a static method, so it is accessible with the class name too.
  • setProperty() method method throws various exceptions at the time of setting the system property
    • SecurityException: In this exception, its checkPermission() method can't allow access to the given system properties when the security manager exists.
    • NullPointerException: In this exception, if the given system_property or given system_property_value is null.
    • IllegalArgumentException: In this exception, if the given system property is null.

Syntax:

    public static String setProperty(
            String  system_property, 
            String system_property_value);

Parameter(s):

  • ssystem_property – represents the name of the system property.
  • ssystem_property_value – represents the value of the system property.

Return value:

The return type of this method is String, it returns the old value of the system property if exists else it returns null.

Example:

// Java program to demonstrate the example of 
// setProperty() method of System Class.

import java.lang.*;
import java.util.*;

public class SetPropertyMethod {
    public static void main(String[] args) {
        //Display previous operating system
        //architecture before setting properties
        System.out.print("Previous os name :" + " ");
        System.out.print(System.getProperty("os.name"));

        System.clearProperty("os.name");
        System.setProperty("os.name", "Ubuntu");

        System.out.println();

        //Display new operating system
        //architecture after setting properties
        System.out.print("New os name :" + " ");
        System.out.print(System.getProperty("os.name"));
    }
}

Output

E:\Programs>javac SetPropertyMethod.java
E:\Programs>java SetPropertyMethod
Previous os name : Linux
New os name : Ubuntu


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.