473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How static variables are stored?

18
Friends,
Jus want to know how static variables r stored and accessed.
I believe that static variable will maintain only single copy per application domain.
If so then how does it allow a single static variable to have different values for diff instance.


Cheers,
Murugs.
Nov 5 '07 #1
18 3090
r035198x
13,262 8TB
Friends,
Jus want to know how static variables r stored and accessed.
I believe that static variable will maintain only single copy per application domain.
If so then how does it allow a single static variable to have different values for diff instance.


Cheers,
Murugs.
Statics are stored once when a class is "loaded" (on the stack). Objects of the same class share one variable that is stored in one memory location for that class. All instances created from that same class (dynamically on the heap) would all have a reference to that same (one memory location) variable which is on the stack.
A single static variable can thus not have different values for different instances of the same class.
Nov 5 '07 #2
Murugs
18
Thanks for your reply.
I meant application instance not class instance.

Class GlobalVariables
{
public static string userName;
}

consider we have one login page.
In Login.aspx.cs page i am assigning some values for userName like

GlobalVariables.userName=txtUserName.Text //saving the user entered text to that static variable.

now the single static variable can have different values for each application instance(opening 2 instance of an application in 2 diff browsers.)

can you explain the above scenario?

Thanks,
Murugs.
Nov 5 '07 #3
r035198x
13,262 8TB
Thanks for your reply.
I meant application instance not class instance.

Class GlobalVariables
{
public static string userName;
}

consider we have one login page.
In Login.aspx.cs page i am assigning some values for userName like

GlobalVariables.userName=txtUserName.Text //saving the user entered text to that static variable.

now the single static variable can have different values for each application instance(opening 2 instance of an application in 2 diff browsers.)

can you explain the above scenario?

Thanks,
Murugs.
Two different applications would run from different memory locations and would have therefore not share the same statics for instance variables.
Nov 5 '07 #4
Murugs
18
Same application opened in 2 different browsers.not 2 different application.
Say for example,I have opened 2 internet explorer.now i am accessing thescripts.com in both the IE's.
Nov 5 '07 #5
Plater
7,872 Expert 4TB
Same application opened in 2 different browsers.not 2 different application.
Say for example,I have opened 2 internet explorer.now i am accessing thescripts.com in both the IE's.
The server is running the applications, not the browser, so it doesn't care who opened what number of browsers.
Seems like you should be storing those values in the Session object, not in a static class anyway.
Nov 5 '07 #6
r035198x
13,262 8TB
Same application opened in 2 different browsers.not 2 different application.
Say for example,I have opened 2 internet explorer.now i am accessing thescripts.com in both the IE's.
The code for the GlobalVariables class runs on the server not the client side. So multiple clients connecting would all have the same instance of the GlobalVariables class (unless it's defined at session level: I'm not sure if that's possible or how that can be done) and therefore share the same value for the userName field.

Edit: And as usual Plater beat me to it ...
Nov 5 '07 #7
Murugs
18
HI Plater,
You have misunderstood my question.
As you said server is running the application,how does the server handles static variable in the above scenorio.Thats my question.
Yes you are correct.we can use session object for that purpose.
My next question is which one is performance wise better (session or static variable) if both can able to maintain instance specific data's.
Thats why i am little bit confused :(

Regards,
Murugs.
Nov 5 '07 #8
Murugs
18
Hi r035198x ,
Yes that is possible.we can store instance specific data in static variables.
I have done a small poc on that and its working.

-Murugs
Nov 5 '07 #9
r035198x
13,262 8TB
HI Plater,
You have misunderstood my question.
As you said server is running the application,how does the server handles static variable in the above scenorio.Thats my question.
Yes you are correct.we can use session object for that purpose.
My next question is which one is performance wise better (session or static variable) if both can able to maintain instance specific data's.
Thats why i am little bit confused :(

Regards,
Murugs.
Read Plater's response again and the one I made after that. The statics are handled in the same way that I've been describing above. Storing values as static variables of a class and storing them in the session are not the same thing. Storing in the static variables is not "able to store instance specific data".
Nov 5 '07 #10
r035198x
13,262 8TB
Hi r035198x ,
Yes that is possible.we can store instance specific data in static variables.
I have done a small poc on that and its working.

-Murugs
I give up then.
You simply can't have two diffrent instances of the same class having different values of the same static variable at the same time in the same application.
Nov 5 '07 #11
Plater
7,872 Expert 4TB
If you have a web application (ASP.NET)
And you have code like:
Expand|Select|Wrap|Line Numbers
  1. public class myclass
  2. {
  3.    public static int Myint=30;
  4. }
  5.  
It is my understanding that there is only ONE instance of that Myint variable, regardless fo what client opens how many browsers.
So setting that value should change it for everyone.

Session is certainly prefered over statics for maintaining information that is only relevant to the "current user"
Nov 5 '07 #12
Murugs
18
Please just do this sample page

class GlobalVariables
{
public static string userName;
public static string sessionValue;
}

aspx page
//Have a textbox by the name txtUserName to allow the user to enter somevalue
//Have a button by the name btnSubmit
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
GlobalVariables.sessionValue=Session.SessionID;
Response.Write(GlobalVariables.sessionValue);
Response.Write("<br>");
GlobalVariables.userName=txtUserName.Text;
Response.Write(GlobalVariables.userName);
}
}
now run http://localhost/test/Default6.aspx in two IE.
Enter some name in the textbox in both the ie's and click the submit button.Now you can able to see GlobalVariables.sessionValue and GlobalVariables.userName has two different values.
How does the static variable can able to maintain 2 different values?
I hope now u can understand my question.
Please run the above sample and send me ur replies.

Cheers,
Murugs.
Nov 6 '07 #13
r035198x
13,262 8TB
Please just do this sample page

class GlobalVariables
{
public static string userName;
public static string sessionValue;
}

aspx page
//Have a textbox by the name txtUserName to allow the user to enter somevalue
//Have a button by the name btnSubmit
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
GlobalVariables.sessionValue=Session.SessionID;
Response.Write(GlobalVariables.sessionValue);
Response.Write("<br>");
GlobalVariables.userName=txtUserName.Text;
Response.Write(GlobalVariables.userName);
}
}
now run http://localhost/test/Default6.aspx in two IE.
Enter some name in the textbox in both the ie's and click the submit button.Now you can able to see GlobalVariables.sessionValue and GlobalVariables.userName has two different values.
How does the static variable can able to maintain 2 different values?
I hope now u can understand my question.
Please run the above sample and send me ur replies.

Cheers,
Murugs.
I can't try that code now with the way I've setup my mono (but mostly because I hate .NET programs passionately) but it doesn't look like you have a static variable holding two different values for different instance objects. All I see is something with the following structure

Expand|Select|Wrap|Line Numbers
  1. string myVal = "first";
  2. myVal = "second";
  3. myVal = "third";
  4. //...
at any one point the variable myVal is always storing one value.
The previous value is being overwritten bythe new value. That is all you are doing in your program. Simple overwritting of the same variable with different values.
Nov 6 '07 #14
Murugs
18
As you said,after execution of line 3 the variable myVal will hold only the value "third".But in my case its maintaining different values.
Thats why i confused abt the storage of Static variables...
Nov 6 '07 #15
Plater
7,872 Expert 4TB
That doesn't actually have two different values.
Each page load stores it's value in that static, then spits it back out to the page.
The next page overwrites that value with it's own, then shows it again.
That is not a valid test.
Nov 6 '07 #16
Murugs
18
Hi Plater,
Just update the static variable on some condition and check it again.
its maintaining different values.
check the below code.

GlobalVariables.userName = "Value not changed";
Response.Write(GlobalVariables.userName);
if (Request.QueryString["Id"] == "1")
{
GlobalVariables.userName="value changed";
Response.Write(GlobalVariables.userName);
}

run the application in two ies.
Now pass the Id =1 as querystring in one ie.

Now check you can have different values.
Let me know if you are not understand my question..pls do some sample before reply so u can able to know my question well.
Thanks..

-Murugs
Nov 6 '07 #17
Plater
7,872 Expert 4TB
Hi Plater,
Just update the static variable on some condition and check it again.
its maintaining different values.
check the below code.

GlobalVariables.userName = "Value not changed";
Response.Write(GlobalVariables.userName);
if (Request.QueryString["Id"] == "1")
{
GlobalVariables.userName="value changed";
Response.Write(GlobalVariables.userName);
}

run the application in two ies.
Now pass the Id =1 as querystring in one ie.

Now check you can have different values.
Let me know if you are not understand my question..pls do some sample before reply so u can able to know my question well.
Thanks..

-Murugs

AGAIN, you are SETTING the value each time and overwriting the previous value. You don't see it happening because it will print it out before the next page is loaded

Create a page with two buttons and label.
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.    MyLabel.Text=GlobalVars.thinger;
  4. }
  5. protected void btChange_Click(object sender, EventArgs e)
  6. {
  7.    GlobalVars.thinger = Request.RawUrl;
  8. }
  9. protected void btView_Click(object sender, EventArgs e)
  10. {//do nothing
  11. }
  12.  
Make sure you have something like:
Expand|Select|Wrap|Line Numbers
  1. public static class GlobalVars
  2. {
  3.    public static string thinger="";
  4. }
  5.  
Then you can do your two broswers test, changing the querystring and using the "change" button to set the global variable and use the View button to view it.
Nov 6 '07 #18
Murugs
18
Atlast got it.its all coz of wrong testing..
Thanks Plater..
Nov 7 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Abhishek Pandey | last post by:
Hi All, A basic question. In C (and C++ also) global and static variables which are un-initalized are guaranteed to be zero. This is because (as per my knowledge) they are all stored in the...
8
by: VJ | last post by:
Hi, I apologize if this question has been asked many times on the group! I am new to programming, I know that there are three section in address space- one for code, one for stack, and the...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
11
by: Murali | last post by:
Hi Can anyone tell me where a static variable be stored. I am sure that it is stored in the data segment of the executable's memory footprint...But in what? a stack or a heap or is it purely...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
4
by: Mantorok | last post by:
Hi I have an ASP app which references a few static properties in some of the classes. I understand that you should use Session variables, but is it "possible" to have each session "not"...
2
by: plmanikandan | last post by:
Hi, I have a doubt of storing static variables in memory.I think static,global variables are stored in the same location.Static,Gloabal variables are stored in HEAP .Can anybody explain me the...
18
by: Jack | last post by:
Thanks.
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.