views:

539

answers:

3

Hello All,

It's well known fact that static variable cannot be used in Web Application as it remains static through out application.But,is there any scenario where static variable can be used in Web Application to achieve the functionality?

Thanks

+1  A: 

You can store your static information in a number of places...

  • In a DB
  • In a cookie
  • In an Application or Session variable
  • In a querystring (or even a form variable)

(Or am I misunderstanding the question?)

CJM
Yeah i understand that static values can be stored in session, View State and the place you mentioned. But Is there any specific funcationality that can be achieved using the Static Variable (I mean i variable declared as Static) instead of storing it other places
Sri Kumar
A: 

You don't mention what web framework you are using, but if you are using ASP.Net, you can assign objects to the Application object or the Session object, which will hold information across the application between requests, and across all requests from an individual user respectively.

If you are asking in a general way, and I suspect you are as this is marked subjective+discussion, then you can try using a cookie to hold the variable across all requests for a user.

As for holding some value for all users, you should have a look at the web framework you are using. You have options such as storing something in a file that is accessible at each request, through to storing something in the equivalent of the Application object of ASP.Net.

I'm sorry if I'm being too pragmatic here, but it's late where I live :).

Khanzor
+1  A: 

I'm not sure if I understand the question, but there are cases in which using a static variable in a web application is perfectly fine, if one understands the implications. For instance, a common usage in .NET web applications using NHibernate and the "session-per-request" model is to keep a static instance of an ISessionFactory around to create new NHibernate sessions. This is useful since session factories are very heavy objects and generally take a lot of resources to create.

Ryan Duffield