• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static method local variables  RSS feed

 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if several threads accessing one static method which is having 2 local variables.

Would those threads will hold their copy of local variables in their stacks?
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, and it doesn't really matter whether that method is static or not.
A local variable is one that's declared (and somehow initialized) inside the method block.
Therefore, any thread that executes that method block will declare and initialize its own variable.
 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more question:

Suppose there is one class Foo:



There is another class:



If several threads calling method Delegate.cookFood() then would thread stacks
will have their own copy of local and instance variables in their stacks?
 
Mazer Lao Tzu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since foo is static in Delegate, each thread that executes the static cookFood method would use the same foo object.

When multiple threads are using the same objects, they share instance variables, but they do NOT share local variables. Instance variables are stored per object instantiating a class. Local variables are stored per invocation of a method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!