Home » Java programming language

Java Properties setProperty() Method with Example

Properties Class setProperty() method: Here, we are going to learn about the setProperty() method of Properties Class with its syntax and example.
Submitted by Preeti Jain, on March 21, 2020

Properties Class setProperty() method

  • setProperty() method is available in java.util package.
  • setProperty() method is used to set the given value element (val_ele) associated with the given key element (key_ele) when no value element associate previously otherwise it replaces the old value with the given value for the given key of this Properties.
  • setProperty() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • setProperty() method does not throw an exception at the time of setting the property.

Syntax:

    public Object setProperty(String key_ele, String val_ele);

Parameter(s):

  • String key_ele – represents the key element at which the value is to be set.
  • String val_ele – represents the value element for the given key.

Return value:

The return type of the method is Object, it returns the old value linked with the given key if exists otherwise it returns null.

Example:

// Java program to demonstrate the example 
// of Object setProperty(String key_ele, String val_ele)
// method of Properties

import java.io.*;
import java.util.*;

public class SetPropertyOfProperties {
 public static void main(String arg[]) throws Exception {
  // Instantiate Properties object
  Properties prop = new Properties();

  prop.put("10", "C");
  prop.put("20", "C++");
  prop.put("30", "JAVA");
  prop.put("40", "PHP");
  prop.put("50", "SFDC");

  // By using setProperty() method is to
  // replace the old value for the given
  // key if exists with the given new value
  Object ob = prop.setProperty("20", "OOPS");

  // Display old value returned for the
  // given key element
  System.out.println("prop.setProperty(20,OOPS): " + ob);

  // Display updated list
  prop.list(System.out);
 }
}

Output

prop.setProperty(20,OOPS): C++
-- listing properties --
50=SFDC
40=PHP
30=JAVA
20=OOPS
10=C


Comments and Discussions!

Load comments ↻





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