Question on static methods

Discussion in 'ASP .Net' started by =?Utf-8?B?RGlmZmlkZW50?=, Jan 14, 2005.

  1. Hello All,

    Consider the following code block...

    Public class ClassA
    {
    public int A;
    public int B;

    public static void MethodA()
    {
    . .. ......
    }
    }

    If two threads execute the method A, would they be having two different
    copies of A and B variables?

    Thanks!!
    =?Utf-8?B?RGlmZmlkZW50?=, Jan 14, 2005
    #1
    1. Advertising

  2. No. When you call a static method, it does not create an instance of ClassA
    and you cannot access instance variables (such as A and B) from within a
    static method.

    --
    Philip Q
    Microsoft MVP [ASP.NET]

    "Diffident" <> wrote in message
    news:...
    > Hello All,
    >
    > Consider the following code block...
    >
    > Public class ClassA
    > {
    > public int A;
    > public int B;
    >
    > public static void MethodA()
    > {
    > . .. ......
    > }
    > }
    >
    > If two threads execute the method A, would they be having two different
    > copies of A and B variables?
    >
    > Thanks!!
    >
    Philip Q [MVP], Jan 14, 2005
    #2
    1. Advertising

  3. The question is not exactly correct. A static method can't use non-static
    members of the class. You would have to declare A and B as static. And then
    the answer is no, there will be only one instance.

    Eliyahu

    "Diffident" <> wrote in message
    news:...
    > Hello All,
    >
    > Consider the following code block...
    >
    > Public class ClassA
    > {
    > public int A;
    > public int B;
    >
    > public static void MethodA()
    > {
    > . .. ......
    > }
    > }
    >
    > If two threads execute the method A, would they be having two different
    > copies of A and B variables?
    >
    > Thanks!!
    >
    Eliyahu Goldin, Jan 14, 2005
    #3
  4. =?Utf-8?B?RGlmZmlkZW50?=

    Joe Fallon Guest

    If your static method creates an instance of the class,
    then you will have access to variables A and B and they will be unique for
    each instance.

    Rocky Lhotka uses this technique to create Business Objects in his CSLA
    framework.
    http://www.lhotka.net/ArticleIndex.aspx?area=CSLA .NET
    --
    Joe Fallon



    "Diffident" <> wrote in message
    news:...
    > Hello All,
    >
    > Consider the following code block...
    >
    > Public class ClassA
    > {
    > public int A;
    > public int B;
    >
    > public static void MethodA()
    > {
    > . .. ......
    > }
    > }
    >
    > If two threads execute the method A, would they be having two different
    > copies of A and B variables?
    >
    > Thanks!!
    >
    Joe Fallon, Jan 15, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    1
    Views:
    506
    Scott Allen
    Jan 7, 2005
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,366
    Tony Morris
    Oct 19, 2004
  3. Oliver Wong
    Replies:
    14
    Views:
    1,591
    Chris Uppal
    Jun 13, 2006
  4. lightning
    Replies:
    4
    Views:
    892
    Daniel Pitts
    Oct 30, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    301
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page