Home »
Java programming language
Java ListResourceBundle getContents() Method with Example
ListResourceBundle Class getContents() method: Here, we are going to learn about the getContents() method of ListResourceBundle Class with its syntax and example.
Submitted by Preeti Jain, on March 22, 2020
ListResourceBundle Class getContents() method
- getContents() method is available in java.util package.
- getContents() method is used to get the contents into the form of an Object array (Object [][]) and every item is pairs of objects in an Object[][] where first indices represent the key element and the second indices represent the value element linked with the key in an object array.
- getContents() 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.
- getContents() method does not throw an exception at the time of getting contents.
Syntax:
protected abstract Object[][] getContents();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is Object [] [], it returns 2D object array that contains two objects in the form of key-value pairs.
Example:
// Java program to demonstrate the example
// of Object[][] getContents() method of
// ListResourceBundle
import java.util.*;
// Instantiates a class that extends
// ListResourceBundle
class GetContents extends ListResourceBundle {
// By using getContent() method is to
// get the contents in the form of
// 2D objects
protected Object[][] getContents() {
return new Object[][] {
{
"10",
"C"
}, {
"20",
"C++"
}, {
"30",
"JAVA"
}, {
"40",
"SFDC"
}, {
"50",
"PHP"
}
};
}
}
public class Main {
public static void main(String[] args) {
// Instantiates an object of
// GetContents
GetContents get_c = new GetContents();
// Display the value for the
// given key element
System.out.println("get_c.getString(40): " + get_c.getString("40"));
}
}
Output
get_c.getString(40): SFDC