How to create a static local variable in Java?

I read that Java does not support static local variables other than C / C ++. Now, if I want to encode a function with a local variable whose value should be stored between function calls, how to do it?
Should I resort to using instance variables?

+44
java scope static
Jan 17 '10 at 3:10
source share
7 answers

You can have a static class variable that will be stored in all instances of the class. If this is what you want. If not, use an instance variable that will be saved only for method calls to this object.

 public class Foo { static int bar; //set bar somewhere public int baz() { return 3 * bar; } } 
+28
Jan 17 '10 at 3:14
source share
β€” -

Local variables are variables inside the method. Only the method can access these variables. you cannot have a static local variable, but you can use instance variables or class variables.

If you have your method as static, which by default creates a copy of this method to all objects and cannot be broken down further, because local variables restrict access only to the way in which they are located.

+5
Aug 19 '13 at 23:06 on
source share

Should I resort to using instance variables?

yes - after all, instance variables are needed for this. They maintain state between calls to the object's methods. The presence of static class variables can lead to the same result, but it can make your program more complex and complicated for testing / layout / support.

+4
Jan 17 '10 at 3:13
source share

If you want to reuse the value of a variable between function calls and isolate this variable from other methods, you must encapsulate it in an object. First, define a class with only "static" variables and the desired function:

 class MyFunctionWithState { private int myVar = 0; public int call() { myVar++; return myVar; } } 

Then from your client code:

 class Foo { private MyFunctionWithState func = new MyFunctionWithState(); public int bar() { return func.call(); } } 

Now, if func relies on the internal state of Foo , you can pass the corresponding data via call() or pass an instance of Foo and let the function call the corresponding getters:

 class MyFunctionWithState { private int myVar = 0; public int call( Foo f ) { myVar += f.getBaz(); return myVar; } } class Foo { private MyFunctionWithState func = new MyFunctionWithState(); public int bar() { return func.call( this ); } public int getBaz() { /*...*/ } } 
+4
Jan 17 '10 at 4:34
source share

Either you noted the correct answer, which was not, or you really need variables of the static class, but the correct answer is basically Levitic, or technically a mixture of all the answers.

For people from a C-background like me, really wanting static function-local variables, solely for the advantages of the region over global ones, it seems like you can't in Java. Not without execution, something like @paradigmatic happens to a class instance with a static global and getter (), unique to the class instance in which you use your function.

If, as I suspect, you are using the class as Singleton or completely static, as a programmer with very complex procedures, we all started, as @Ellie P or @ user2626445 answer will work fine, since no other instance will blame your global one. But in order to achieve what you think you want, it should be just an instance variable that is global in C. They are stored in all function calls, but allow you to use your function in the OO method, so several instances can save their own own constant variables for this particular function.

Personally, I will get around this in jave just like Hi-Tech C, by not allowing function bits to be variable by declaring a global value in the file just above the function declaration. So, when I crack the code later, I’m less likely to think that it’s global, and the temptation to work with it is β€” don’t we encapsulate things?

0
Jan 18 '16 at 12:21
source share

In the following example, count behaves like a static local variable that you can use in C:

 public class Foo { private int count = 0; public int getCount() { return count++; } } 

There are no static local variables, such as support for other languages. Since java is very "class" -oriented, it tries to bring it into this context. If you want to simulate this behavior, you use an instance variable that is used only by this method. Thus, it is static for different method calls, and not for the class itself.

-one
Dec 19 '14 at 9:13
source share

final int intVar = 1; // Will be entered only once. Behaves like a static local var C ++ in a method.

-3
Jul 10 '14 at 2:03
source share



All Articles