1

I know that in C, when the keyword static is used on a local variable, it causes that variable to remain initialized between function calls (i.e. when the variable goes out of scope). For example:

int myFunction() {
  static int i = 3;
  i++;
  return i;
}

If myFunction() is called twice, it will return 4 the first time and 5 the second time (because i keeps its value between the two calls rather than being reinitialized the second time).

My question is this: does Java have an equivalent keyword to static in C? Java also has the keyword static, but it is used completely differently than in C.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Gus Wiedey
  • 47
  • 3
  • If `static` is one the class level scope, it will act as you want. – Scary Wombat Aug 23 '16 at 00:20
  • Static variables di – Ricardo Leon Aug 23 '16 at 00:20
  • 3
    For some reason people seem to misunderstand the question. The question is not what `static` means in Java. The question is: *"Can you accomplish in Java (preferrably with a simple keyword) what the `static` keyword does in C?"* – RaminS Aug 23 '16 at 00:31
  • @Gendarme I don't think all of the other posters (myself included) misunderstood the question, I think you are misunderstanding the responses. There is no analogous keyword or concept in Java. This is why the top up-voted responses are also "no" and then they propose options that admittedly don't produce the same behavior as C. The issue here is that `static` in C is overloaded with more than one meaning, which depends upon on context. It was a conscious decision not to carry this confusion into other languages. – EntangledLoops Jun 13 '20 at 19:46

5 Answers5

4

Not exactly, but a private static class level variable will almost do the same thing.

But

  • it will be visible to all other methods of the class as well

  • it will be initialized not on first method call, but when the class itself is loaded

I suppose that is workable.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • `private` alone should be sufficient, and probably more preferable. – Code-Apprentice Aug 23 '16 at 00:30
  • 2
    Well, you still need to make it `static` if you want to call it from a `static` method (which is what a `C` function is). – Thilo Aug 23 '16 at 00:31
  • True. However, it might be better to make the method non-static in the first place. Unfortunately, we don't have enough information about the design to give informed suggestions about `static` verses `non-static`. – Code-Apprentice Aug 23 '16 at 00:33
  • Or, put the other way around, if you don't make it `static`, there will be one value per object instance, i.e. multiple values, which is also quite different from what a `static` variable does in `C`. If you are talking "object instances" then you are close to `C++`, which also allows `static` variables, and they don't "multiply" either. – Thilo Aug 23 '16 at 00:34
  • That's a good point. I still suspect that there are details missing here that would help suggest what design decisions to make to translate from procedural C style to Object Oriented Java style. – Code-Apprentice Aug 23 '16 at 00:36
3

All variables in a method are local to the function and placed on the stack. The closest you have is a static variable in a class.

If you make the variable private and place the method in a class of it's own you will achieve much the same result. (With a private constructor)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

It's somewhat similar to the private keyword, as in C a static global variable or a function is visible only in the file it's declared in...

So that's probably the closest you will find :)

Arthur Ceccotti
  • 380
  • 2
  • 6
-2

No, because all methods and functions are bound to classes in Java, so there is no "global space" as there is in C. The static keyword in Java has different semantics.

See the docs for static and this post for more detail on the differences.

EntangledLoops
  • 1,951
  • 1
  • 23
  • 36
  • 1
    He's not asking about global static variables, he's talking about local static variables within a function. – Barmar Aug 23 '16 at 00:21
  • There is no such thing as a static variable in a function in Java---that's not legal syntax. The answer remains "no". – EntangledLoops Aug 23 '16 at 00:21
  • 1
    He's not talking about the Java `static` keyword, he's asking if there's something analogous to the C concept. Of course it will have different syntax. – Barmar Aug 23 '16 at 00:24
  • 1
    I understand the question fully. There is no equivalent keyword, hence my answer "no". You can simulate it as you wish with `private` and member vars, etc. but it is not the same and should not be confused. – EntangledLoops Aug 23 '16 at 00:25
  • The problem I have with your answer is the part after "because". The reason is just that it doesn't exist, it has nothing to do with functions being bound to classes. PHP has functions bound to classes, but it allows `static` variables inside those functions, so the two features aren't mutually exclusive. – Barmar Aug 23 '16 at 00:27
  • 1
    C++ also allows static variables inside methods. – Barmar Aug 23 '16 at 00:27
  • This question isn't about C++ or PHP, so those are irrelevant. I understand your criticism of the answer, but I was drawing the poster to the true reason the keyword doesn't exist, which is that there is no keyword to force local vars onto the heap vs. the stack w/o an invocation of `new`. Please see the links I posted before commenting again. – EntangledLoops Aug 23 '16 at 00:30
  • The "true reason it doesn't exist" is just that the Java designers didn't feel like putting it into the language. You can't say it's because of some other feature of the language. – Barmar Aug 23 '16 at 00:33
  • I posted the answer to his question and didn't come here to start a war with you on the keyword `static`. The Java developers "feelings" had nothing to do with the decision to not include the keyword in that usage, it was an intentional decision which---once again---you would know if you had bothered to read either link I posted instead of downvoting for essentially no reason. – EntangledLoops Aug 23 '16 at 00:36
  • I've glanced at those links. I don't see how the description of existing features can provide the reason for something *not* being in the language. And I haven't downvoted the answer, that was someone else. – Barmar Aug 23 '16 at 00:38
-2

Java uses static in a different manner. To get the same result you want here, you should use a private field instead.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268