How static variables are stored?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Murugs
    New Member
    • Nov 2007
    • 18

    How static variables are stored?

    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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Murugs
    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.

    Comment

    • Murugs
      New Member
      • Nov 2007
      • 18

      #3
      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=txtUs erName.Text //saving the user entered text to that static variable.

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

      can you explain the above scenario?

      Thanks,
      Murugs.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Murugs
        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=txtUs erName.Text //saving the user entered text to that static variable.

        now the single static variable can have different values for each application instance(openin g 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.

        Comment

        • Murugs
          New Member
          • Nov 2007
          • 18

          #5
          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.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by Murugs
            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.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Murugs
              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 ...

              Comment

              • Murugs
                New Member
                • Nov 2007
                • 18

                #8
                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.

                Comment

                • Murugs
                  New Member
                  • Nov 2007
                  • 18

                  #9
                  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

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Murugs
                    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".

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by Murugs
                      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.

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        If you have a web application (ASP.NET)
                        And you have code like:
                        Code:
                        public class myclass
                        {
                           public static int Myint=30;
                        }
                        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"

                        Comment

                        • Murugs
                          New Member
                          • Nov 2007
                          • 18

                          #13
                          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.Configur ation;
                          using System.Collecti ons;
                          using System.Web;
                          using System.Web.Secu rity;
                          using System.Web.UI;
                          using System.Web.UI.W ebControls;
                          using System.Web.UI.W ebControls.WebP arts;
                          using System.Web.UI.H tmlControls;

                          public partial class Default6 : System.Web.UI.P age
                          {
                          protected void Page_Load(objec t sender, EventArgs e)
                          {

                          }
                          protected void btnSubmit_Click (object sender, EventArgs e)
                          {
                          GlobalVariables .sessionValue=S ession.SessionI D;
                          Response.Write( GlobalVariables .sessionValue);
                          Response.Write( "<br>");
                          GlobalVariables .userName=txtUs erName.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.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by Murugs
                            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.Configur ation;
                            using System.Collecti ons;
                            using System.Web;
                            using System.Web.Secu rity;
                            using System.Web.UI;
                            using System.Web.UI.W ebControls;
                            using System.Web.UI.W ebControls.WebP arts;
                            using System.Web.UI.H tmlControls;

                            public partial class Default6 : System.Web.UI.P age
                            {
                            protected void Page_Load(objec t sender, EventArgs e)
                            {

                            }
                            protected void btnSubmit_Click (object sender, EventArgs e)
                            {
                            GlobalVariables .sessionValue=S ession.SessionI D;
                            Response.Write( GlobalVariables .sessionValue);
                            Response.Write( "<br>");
                            GlobalVariables .userName=txtUs erName.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

                            [CODE=cpp]string myVal = "first";
                            myVal = "second";
                            myVal = "third";
                            //...[/CODE]
                            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.

                            Comment

                            • Murugs
                              New Member
                              • Nov 2007
                              • 18

                              #15
                              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...

                              Comment

                              Working...