19

Assume the following:

private static boolean A()
{
  int parsedUntil = 0;
  ...
  ...
  ...
}

Is parsedUntil considered to be a static variable? I noticed that I can't declare it as static inside this static function.

Follow-up question: I read that a static variable will only be initialized once. Does that mean the first time I call function A() the value will be set to zero, but every other time I call A(), that row is omitted?

Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
Thomas Johansson
  • 583
  • 2
  • 6
  • 15

5 Answers5

28

No, it's not a static variable. It's a local variable. Any variable declared in a method is a local variable. If you want a static variable, you have to declare it outside the method:

private static int parsedUntil = 0;

There's no way of declaring a static variable which can only be used within a single method.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    It's all about scope. The scope is local to the method, regardless of the method type. – Robin Jun 21 '11 at 14:01
  • 2
    @Robin: No, it's about lifetime. In other langauges, "static locals" can have a single-method scope but static lifetime, which is sometimes useful; in both cases the scope is local to the method, but the lifetime is different. – configurator Jun 22 '11 at 00:11
  • I read that, "A static method can access only static data. It cannot access non-static data (instance variables)" then how come `parsedUntil` being a non-static variable be accessible from a static function? – Saurabh Rana Apr 09 '21 at 06:26
  • 1
    @SaurabhRana: Assuming you mean `parsedUntil` in the original question (rather than in my answer) that's a local variable, not an instance variable. I don't know where you got that exact sentence from, but it's not the way I'd have written it. – Jon Skeet Apr 09 '21 at 06:29
  • @JonSkeet makes sense. So static methods can 'access' other static methods and variables, but of course static methods can have local variables. `parsedUntil` is a local variables in the static method. – Saurabh Rana Apr 09 '21 at 20:22
6

no, A() is a static method, and parsedUntil is a local variable inside A.

Modifiers like static are not valid in local variables (only final is permitted afaik)

Follow-up question: I read that a static variable will only be initialized once.

true

Does that mean the first time I call function A() the value will be set to zero, but every other time I call A(), that row is omitted?

since parsedUntil is not a static field, but a local variable in a static method, this is not the case.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • But what would it mean if I declared it as final inside the function? Regarding classes I read that the final keyword means that the functions of such a class, cannot be overloaded by subclasses. (Perhaps such classes cannot even be extended!?) – Thomas Johansson Jun 21 '11 at 14:05
  • in local variables, final means that the variable can't be re-assigned – Sean Patrick Floyd Jun 21 '11 at 14:08
5

static variables cannot be declared locally inside methods - they can only be members of a class, and they get initialised when the class is loaded.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
1

Java does not have static local variables like C or C++ does, so you can never have static int parsedUtil = 0;.

So no, parsedUtil is not in any sense "static". Its value is initialised to 0 every time the method is executed.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Local variables are NOT initialised in Java. – Ian Oct 31 '16 at 20:21
  • @Ian - According to the JLS 14.4.2: "*If a [local variable] declarator has an initializer, the initializer is evaluated and its value is assigned to the variable.*" That's close enough for me ;) – Oliver Charlesworth Oct 31 '16 at 21:47
  • Fair point. But, unlike variables at the class or instance scope Java performs no initialization by **default**. That was the point I was trying to make, though rereading your answer I see it doesn't actually apply in this case. My bad. – Ian Nov 01 '16 at 00:11
1

No it's not C.

parsedUntil is not static. It's just a local variable. You cannot declare static variable inside the method.

Regarding second question - static variables can be assigned as many times as you want. You cannot reassign only final variables.