When you create an instance of a class, all the variables in that instance is specific to that instance and get killed when the instance is out of scope. But how does it work in a static method? Suppose two people call System.Math.Abs() at exactly the same time. How does the runtime differentiate between the two callers? Is this where threading comes in? Are separate threads automatically created for each caller?
Share
There is no real difference between static and non-static methods in terms of method-variable lifetime. In both cases, as an implementation detail, locals are typically (not quite always: there are exceptions) allocated on the stack. The stack is per-thread, so local method variables do not crossover between threads.
The only difference here between instance and static is that instance methods have an implicit zeroth parameter, aka “this”, that is pushed by the caller (plus some virtual dispatch and null-check fun).
For simplicity, I’m glossing over iterator blocks, captured variables, etc.