I have a question about the variables inside the static method.
Do the variables inside the static method share the same memory location or would they have separate memory?
Here is an example.
public class XYZ
{
Public Static int A(int value)
{
int b = value;
return b;
}
}
If 3 different user calls execute the method A
XYZ.A(10);
XYZ.A(20);
XYZ.A(30);
at the same time. What would be the return values of each call?
XYZ.A(10)=?
XYZ.A(20)=?
XYZ.A(30)=?
They’re still local variables – they’re not shared between threads. The fact that they’re within a static method makes no difference.
If you used a static variable as the intermediate variable, that would be unsafe:
Here, all the threads would genuinely be using the same
bvariable, so if you called the method from multiple threads simultaneously, thread X could write tob, followed by thread Y, so that thread X ended up returning the value set by thread Y.