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条回答
可以哭但决不认输i
2楼-- · 2019-01-04 13:24

Should I resort to using instance variables?

yes - after all, that is what instance variables are for. They store the state between invocations of the methods of the object. Having static class variables can sort of achieve the same result, but can make your program more coupled and harder to test/mock/maintain.

查看更多
登录 后发表回答