• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static objects

 
Suresh Appagundi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following program with static block inside
public class Test {

private static HashMap hMap;
static {
hMap = new HashMap();
hMap.put(new Integer(1),"DDD")
hMap.put(new Integer(2),"HHH")
hMap.put(new Integer(3),"JJJ")
}

public Test(){}
public static String getValue(int iKey) {
// which will return the value for the key passed in,
// fetching it from HashMap.
}
}
// Imports are not shown

Iam using above program in my web application. I want hMap object to be there for ever until i restart my web server.
Now if the hMap is not used for long time and if garbage collector releases this hMap (remember hMap is a static object) to allocate memory for some other object, my getValue method will give error.
Or is it that there will be seperate memory storage area for static objects or variables(static storage memory other than heap), which will hold only static objects or variables until the application is running.
Thanks in advance
 
Rajan Punchouty
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you said u r using it in your web application... u can definitly put ur object in application context scope....and access it from any servlet or jsp and will be available thru out.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesus Christ,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Servlets forum, as that's about the closest forum we have for general web application questions.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables don't get garbage collected. This can lead to memory leak type situations if hMap gets too big. You don't have to worry about errors though because the static variables will keep their values until you restart the app.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic