Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static instance variable in a Java class

 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
I have a servlet which at initialisation reads some properties and their values from a file.
These key values are then stored in a HashMap
This hashmap is defined as an instance level static variable in a class.



My question is how is this HashMap available across the application even though I have not saved it in session / application ?

So does this mean that static variables once initialised remain available
till the server ( appserver / servlet engine is bounced / restarted ? )

I believe this is related to class loaders / class loading

Could someone explain this ?
Alternatively rather than a solution straight up it owuld be worthwile if someone could point me to a location where I could read this further ?

Regards ,
-anagha
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anagha patankar:
So does this mean that static variables once initialised remain available
till the server ( appserver / servlet engine is bounced / restarted ? )


Yes. It means once it is intialized it would be available until that servlet got out of service.


Could someone explain this ?
Alternatively rather than a solution straight up it owuld be worthwile if someone could point me to a location where I could read this further ?


I like this sentence. I wish everybody were have this kind of approach. It must be appreciated.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anagha patankar:

My question is how is this HashMap available across the application even though I have not saved it in session / application ?


It wouldn't be.
It would only be available to that servlet.
In order to make it available across the application, you would want to bind it to context scope.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
It wouldn't be.
It would only be available to that servlet.
In order to make it available across the application, you would want to bind it to context scope.


Although, I have almost never use static variables in my servlet. Wouldn't we access those like, MyServlet.myStaticVariable??

[Edited]
And off course I am assuming it public.

Thanks.
[ December 28, 2005: Message edited by: Adeel Ansari ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anagha patankar:
This hashmap is defined as an instance level static variable in a class.


Instance variables can't be static. Static variables are class variables. Please dont be confused, its just the way we call it. Think logically you will get it.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Instance variables can't be static.


There is no rule that servlet instance variables shouldn't be static.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you could access it through that servlet.
You could also implement a class as a singleton which would allow access to all that share the same class loader.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishnu Prakash:
There is no rule that servlet instance variables shouldn't be static.


You didn't get me. I am not just talking about servlets. Its just the matter of words.

Why we say instance variable?
Is there any difference between instance and class variables?

Instance variables are variables defined at the class level without the word static. Static variables are class variables because there is a single copy of that variable for all the instances.

Are you getting me?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Yes, you could access it through that servlet.
You could also implement a class as a singleton which would allow access to all that share the same class loader.


Ben, can you please explain your former reply. Just curious because in that you say no and here you are saying yes. Its a bit confusing.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant was that you were correct.

You could access it as a public field of that servlet class using:
ServletName.variableName providing that the field has modifier that allows access.

If you wanted to access without calling it via the servlet class name, you would bind it to context scope.

Also, if you intend to make it available to other objects within your app, it would be better to do so by binding it to one of the scope objects or to make it a singleton. Making it a public field of a servlet does not really follow the spirit of encapsulation or good OOP principals.

Another good reason not to make resources available this way is the first sentence in SRV.2.3.4 of the servlet spec:


SRV.2.3.4 End of Service
The servlet container is not required to keep a servlet loaded for any particular
period of time. A servlet instance may be kept active in a servlet container for a
period of milliseconds, for the lifetime of the servlet container (which could be a
number of days, months, or years), or any amount of time in between....


There is really no guarantee that there will be an instance of the given servlet at the time the the map is needed by the other objects.
[ December 28, 2005: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishnu Prakash:


There is no rule that servlet instance variables shouldn't be static.


It's not a rule.
By definition, static variables can not be static.

"Instance Variable": a variable belonging to an instance of a class.
"Static Variable": = a variable common to ALL instances of a class.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:

By definition, static variables can not be static.

Java is so Zen.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
If you wanted to access without calling it via the servlet class name, you would bind it to context scope.


Then you need to get it from respective context.


Also, if you intend to make it available to other objects within your app, it would be better to do so by binding it to one of the scope objects or to make it a singleton. Making it a public field of a servlet does not really follow the spirit of encapsulation or good OOP principals.

There is really no guarantee that there will be an instance of the given servlet at the time the the map is needed by the other objects.

Yes Ben, I know that. Thanks for the references. And thats why I said, "I have almost never use static variables in my servlets".

Thanks for the helpful response.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18154
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll admit to using a few static servlet variables in my time, but bear in mind that the servlet spec allows for the creation and destruction of servlet instances in more ways than the average appserver actually tends to do so, so for maximum conformance (and portability), it's best not to depend on a static variable unless you plan to allow for such eventualities.

The other reason for not using statics too freely is that a static variable is shared between all instances of the class but only for that JVM. If you are running a cluster or even a single appserver that has multiple JVMs (though at the moment I don't know of any), use the application context instead, so that the variable will track across JVMs (unless you specifically need it not to do so).

Also keep in mind that IBM and Sun have experimented with "super" JVM's where you could have a single JVM instance carved into partitions (or "virtual" virtual machines??? ) so the dividing lines could lead to unfortunate incidents when these beasties become more common.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic