Home » Java programming language

Java Properties propertyNames() Method with Example

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

Properties Class propertyNames() method

  • propertyNames() method is available in java.util package.
  • propertyNames() method is used to return the set of all the keys that exist in this Properties list and it includes unique keys in default property list in the form of an Enumeration.
  • propertyNames() 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.
  • propertyNames() method may throw an exception at the time of returning property names.
    ClassCastException: This exception may throw when any of the existing keys in this Property list is not compatible with the string

Syntax:

    public Enumeration propertyNames();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is Enumeration, it returns an enumeration of all the keys exist in this Property list along with unique keys exist in the default property list.

Example:

// Java program to demonstrate the example 
// of Enumeration propertyNames() method 
// of Properties

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

public class PropertyNamesOfProperties {
 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 propertyNames() method is to
  // returns the keys set in the form of an
  // Enumeration
  System.out.println("prop.propertyNames(): ");

  for (Enumeration en = prop.propertyNames(); en.hasMoreElements();)
   System.out.println(en.nextElement());
 }
}

Output

prop.propertyNames(): 
40
50
10
20
30


Comments and Discussions!

Load comments ↻





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