File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlets and Static variables... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets and Static variables..." Watch "Servlets and Static variables..." New topic

Servlets and Static variables...

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I know that a servlet is multithreaded... However, I am wondering about the staic varibales... I know that if you declare a variable static that it will be one instance of that variable to share across each servlet that is instantiated...

But does it save me anytime processing time...

Let's say that I have an engine object that in my servlet I pull from the session as well as a dbPool (somethe to make db connections). I should make those static, instead of pulling them out of the session each time... But does it save me any time?
Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
And just to add to that, I pull everything out from the session and store it in variables inside my init() method... But The variales that are declared as emmber variables of the servlet are not static... They won't change so they need to be static but does it by me anything performace wise?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Neither of the schemes you mentioned will save any measureable time.
Compared with all the work that goes on in interpreting a request and creating a response, all tricks like this are insignificant and will only make your code harder to understand.
Go for clarity of code - make it work, then if it is too slow, profile to understand why. Premature optimization is the root of all evil.
It is sorta covered in the JavaRanch Style Guide.
subject: Servlets and Static variables...
jQuery in Action, 3rd edition