Java.util.Properties.setProperty(String key,String value) Method



The java.util.Properties.setProperty(String key,String value) method calls the Hashtable method put. Provided for parallelism with the getProperty method. Enforces use of strings for property keys and values. The value returned is the result of the Hashtable call to put.


Following is the declaration for java.util.Properties.setProperty() method

public Object setProperty(String key,String value)


  • key -- the key to be placed into this property list.

  • value -- the value corresponding to key.

Return Value

This method returns the previous value of the specified key in this property list, or null if it did not have one.


  • NA


The following example shows the usage of java.util.Properties.setProperty() method.

package com.tutorialspoint;

import java.util.*;

public class PropertiesDemo {

   public static void main(String[] args) {
   Properties prop = new Properties();

   // add some properties
   prop.setProperty("Height", "200");
   prop.put("Width", "1500");
   // print the list 
   System.out.println("" + prop);

   // change the properties
   prop.setProperty("Width", "15");
   prop.setProperty("Height", "500");

   // print the list 
   System.out.println("" + prop);

Let us compile and run the above program, this will produce the following result:

{Width=1500, Height=200}
{Width=15, Height=500}