tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Properties > Default Properties

Default Properties 

Default properties provide a clean way to specify alternative or default properties when a certain properties are missing. This is expected behavior in all enterprise applications. The following example shows instantiating and accessing default Properties,

File Name  :  
com/bethecoder/tutorials/utils/props/DefaultProps.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.props;

import java.util.Properties;

public class DefaultProps {
  /**
   @param args
   */
  public static void main(String[] args) {

    //Instantiate default properties
    Properties defaultProp = new Properties();
    defaultProp.setProperty("ONE""111111");
    defaultProp.setProperty("TWO""222222");
    
    //If some property is missing in 'prop'
    //and available in 'defaultProp' then
    //it gets from 'defaultProp' 
    Properties prop = new Properties(defaultProp);
    prop.setProperty("TWO""333333");
  
    // 111111 (value from default properties)
    System.out.println(prop.getProperty("ONE"));
    
    // 333333 (value from properties)
    System.out.println(prop.getProperty("TWO"));  
    
    // null   (value not found in properties & default properties)
    System.out.println(prop.getProperty("FIVE"));    
    
    //Another alternative to specify default value
    //when certain property is missing
    System.out.println(prop.getProperty("FIVE""555555"));  //555555
  }
}
   

It gives the following output,
111111
333333
null
555555



 
  


  
bl  br