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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.