> i will make it class singleton , store it in static
> HashMap but how to access it in other ordinary
> class.....
? By importing the Singleton class and calling its static method . Something like:
// get the static hashmap of the Singleton "PropertyManager"
PropertyManager.getProperties(),
Do you know how to code a Singleton?
> means we have to read file every time Know but i dont
> want to be happen that ?
? No you don't . You read the file once. You store it in the hashmap. Then you hand that hashmap to whatever class needing the data.
getProperties() returns the hashmap, doesn't read the file.
Maybe I should have called the method getMap or something.
Message was edited by:
karma-9
> can u write step by step code including accessing
> HashMap in other class pls
No, I won't.
I do not spoon-feed people. Maybe you'll have better luck with others.
Read the Singleton pattern, and come back when you have written some code and have specific problem(s).
I think that the resources are already cached, at least the ones from ResourceBundle.
The specification of ResourceBundle says:
"...Resource bundle instances created by the getBundle factory methods
are cached by default, and the factory methods return the same resource
bundle instance multiple times if it has been cached...."
[]
> it is not a servlet it is a ordinary class , then how
> to do it?
I'm sorry, but then why did your original post say:
I dont want to read properties file every time , and want to get property value in any servelet or jsp page.
Also, the singleton is your solution. And no, I am not going to post you the code either. There are plenty of Properties examples and Singleton examples (poosibly even Singleton Properties examples) available, Google for some.
my code is as folllows:
public class ClassicSingleton {
private static HashMap managers = new HashMap();
private static ResourceBundle instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static readProperty getInstance() {
if(instance == null) {
instance = new ResourceBundle ();
/* logic to store property file in Static HashMap that is in variable managers*/
}
return instance;
}
public getHashMap(){
return managers;
}
//now i will call getHashMap() method in other classes like ....
}
class A{
void call(){
ClassicSingleton s=new ClassicSingleton ();
s.getHashMap();//calling HashMap
}
}
I prefer this style
public class ClassicSingleton {
private static HashMap managers = new HashMap();
private static ResourceBundle instance = null;
private ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static readProperty getInstance() {
if(instance == null) {
instance = new ResourceBundle ();
/* logic to store property file in Static HashMap that is in variable managers*/
}
return instance;
}
public static getHashMap(){
return managers;
}
//now i will call getHashMap() method in other classes like ....
}
class A{
void call(){
ClassicSingleton.getHashMap();//calling HashMap
}
}
Any objections?