Tuesday, October 7, 2025
HomeLanguagesJavaProperties clear() method in Java with Examples

Properties clear() method in Java with Examples

The Java.util.Properties.clear() method is used to remove all the elements from this Properties instance. Using the clear() method only clears all the element from the Properties and does not delete the Properties. In other words, we can say that the clear() method is used to only empty an existing Properties.

Syntax:

public void clear()

Parameters: The method does not take any parameter

Return Value: The function does not returns any value.

Below programs illustrate the Java.util.Properties.clear() method.

Example 1:




// Java code illustrating clear() method
  
import java.util.*;
  
class PropertiesDemo {
    public static void main(String arg[])
    {
        Properties gfg = new Properties();
        Set URL;
        String str;
  
        gfg.put("ide",
                "ide.geeksforgeeks.org");
        gfg.put("contribute",
                "write.geeksforgeeks.org");
        gfg.put("quiz",
                "www.geeksforgeeks.org");
  
        // checking what's in table
        System.out.println("Current Properties: "
                           + gfg.toString());
  
        System.out.println("\nClearing the Properties");
        gfg.clear();
  
        // checking what's in table now
        System.out.println("New Properties: "
                           + gfg.toString());
    }
}


Output:

Current Properties: {contribute=write.geeksforgeeks.org, quiz=www.geeksforgeeks.org, ide=ide.geeksforgeeks.org}

Clearing the Properties
New Properties: {}

Example 2:




// Java code illustrating clear() method
  
import java.util.*;
  
class PropertiesDemo {
    public static void main(String arg[])
    {
        Properties gfg = new Properties();
        Set URL;
        String str;
  
        gfg.put(1, "Geeks");
        gfg.put(2, "Lazyroar");
        gfg.put(3, "Geek");
  
        // checking what's in table
        System.out.println("Current Properties: "
                           + gfg.toString());
  
        System.out.println("\nClearing the Properties");
        gfg.clear();
  
        // checking what's in table now
        System.out.println("New Properties: "
                           + gfg.toString());
    }
}


Output:

Current Properties: {3=Geek, 2=Lazyroar, 1=Geeks}

Clearing the Properties
New Properties: {}

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html#clear–

RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6709 POSTS0 COMMENTS
Nicole Veronica
11874 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7091 POSTS0 COMMENTS
Thapelo Manthata
6781 POSTS0 COMMENTS
Umr Jansen
6785 POSTS0 COMMENTS