File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes static instance variable in a Java class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "static instance variable in a Java class" Watch "static instance variable in a Java class" New topic
Author

static instance variable in a Java class

anagha patankar
Ranch Hand

Joined: Dec 26, 2005
Posts: 53
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Nov 15, 2004
Posts: 1026

Instance variables can't be static.


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


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Ben Souther:

By definition, static variables can not be static.

Java is so Zen.


There is no emoticon for what I am feeling!
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Jun 25, 2001
Posts: 16062
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static instance variable in a Java class