How do I create a static local variable in Java?

2019-01-04 12:37发布

I've read Java does not support static local variables unlike C/C++. Now if I want to code a function with a local variable, whose value should persist between function calls, how do I do that?
Should I resort to using instance variables?

7条回答
ゆ 、 Hurt°
2楼-- · 2019-01-04 13:04

You can have a static class variable, which will be preserved across all instances of the class. If that's what you want. If not, use an instance variable, which will only be preserved across method calls on this object.

public class Foo {
   static int bar;
   //set bar somewhere

   public int baz() {
      return 3 * bar;
   }
} 
查看更多
Lonely孤独者°
3楼-- · 2019-01-04 13:04

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

public class Foo {

   private int count = 0;

   public int getCount() {
       return count++;               
   }
} 

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

查看更多
时光不老,我们不散
4楼-- · 2019-01-04 13:06

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

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 either pass the relevant data through call() or pass an instance of Foo and let the function call the appropriate 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() {  /*...*/  }
 }
查看更多
萌系小妹纸
5楼-- · 2019-01-04 13:08

final int intVar = 1; // Will be inited only once. Behaves as C++ static local var in the method.

查看更多
放我归山
6楼-- · 2019-01-04 13:15

Either you marked an answer correct that wasn't, or you actually wanted static class variables - but the right answer is basically Levit's, or technically a mixture of all the answers.

For people from a C background like me, truly wanting static function-local variables purely for the scope advantage over globals, it seems the answer is you can't in Java. Not without doing something like @paradigmatic is going for with an instance of a class with a static global and getter() unique to the instance of the class you are running your function in.

If, as I suspect, you're running the class as a Singleton, or completely statically like the inately procedural programmer we all started as, @Ellie P's or @user2626445's answer would work fine since no other instance is going to screw up your global. But to achieve what I think it is you want it should actually be just an instance variable, which is a global in C. They persist across function calls but allow your function to be used in an OO way, so multiple instances can keep their own persistent variables for this particular function.

Personally, I get around this in jave the same way I get around Hi-Tech C not allowing function bit variables, by declaring the global in the file just above the function declaration. That way me when I hack the code later is less likely to think it's a global and be tempted to mess with it - isn't that why we encapsulate things?

查看更多
Rolldiameter
7楼-- · 2019-01-04 13:20

Local Variables are variables inside a method. Only method gets to 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 the objects and cant be broken down any further as local variables limit their access only to the method in which they reside.

查看更多
登录 后发表回答