Local variables in java -


I went through the local variable and class variable concept.

But I was stuck in a doubt - "Why is it that we can not declare as local variable as static ? < For example - let's say that we have a play () function:

  zero play () {static int i = 5; println (i);}  / Code>  

This gives me an error in eclipse: Invalid modifier for parameter I ;

I suspected this That I have read the following concepts:

  1. Variables within the law: the field It is locally ie ie within that method
  2. When the variable is declared as stable, then it is present for the entire class. I is not for a specific object.

Please help me clarify any concept.

Thank you.

Because the scope of the local variable is limited to the surrounding blocks, this is the reason why they can not be referred to other classes or methods (new Statically, nor non-steady).

Wikipedia says that static local variable (for example in C ++):

Static local variables declare within a function , Such as automatic local variables, they have the same general local variable, which differs only in "storage period": puts the function in static local variables during any call, whatever the function is again Called will still exist.

This is not present in Java and in my opinion - for better sake.


Comments