< Prev
Next >

Properties Class

Properties is a legacy class and it extends HashTable legacy class.

Important point about Properties class

While creating an object of Properties, we cannot specify the type of objects Properties class will hold, as it can only hold String type of objects, for example -

Properties prop = new Properties();

Note :

Just like its parent class(HashTable), Properties class also stores the data by using the hash code of its keys and hence the order in which its elements are stored and displayed is not guaranteed.


Some legacy methods in Properties class.

As Properties is a subclass of HashTable legacy class, so some of its methods are inherited by HashTable class. While some of its own specific methods make it a specialized in setting new properties or accessing existing non-system or system properties.

Methods Description
int size() Returns the total number of elements in Stack.
boolean empty() Returns true if Stack is empty, else false.
String getProperty(String key) Pushes the element on the top of Stack.
Object setProperty(String key, String value) Sets a new property in System.
void list(PrintStream out) Sends the list of properties to the output stream.

Let's understand Properties class by an example

import java.util.*;

class Properties1
public static void main(String... ar)
Properties prop= new Properties();
prop.setProperty("Football", "Pele");
prop.setProperty("Golf", "Tiger Woods");
prop.setProperty("Formula 1", "Michael Schumacher");
prop.setProperty("Basketball", "Michael Jordan");
prop.setProperty("Boxing", "Mohammad Ali");

System.out.println("Size of properties = "+ prop.size());

System.out.println("Value associated with Key, Golf = "+ prop.getProperty("Golf"));

//using list() method to send the list of properties on the output stream, "System.out"



Output is :

Size of properties = 5
Value associated with Key, Golf = Tiger Woods
-- listing properties --
Boxing=Mohammad Ali
Basketball=Michael Jordan
Golf=Tiger Woods
Formula 1=Michael Schumacher

Program Analysis

Note :

To display all the properties set in our system like version of OS, JVM, environment variables etc, we should call properties() method of System class, which also returns an object of Properties class -:

 Properties prop= System.getProperties();

and using this prop reference variable of Properties type, we can display all the properties set in our system(it can be a long list).

Please share this article -

< Prev
Next >
< Hashtable Class
Events and Event Classes >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition