Question on static methods

G

Guest

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!!
 
P

Philip Q [MVP]

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.
 
E

Eliyahu Goldin

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top