20

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?

5 Answers 5

29

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It's all about scope. The scope is local to the method, regardless of the method type.
@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.
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?
@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.
@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.
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.

2 Comments

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!?)
in local variables, final means that the variable can't be re-assigned
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.

1 Comment

Thanks! When you say that they can only be members of a class I remember that I have read that and it makes sense!
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.

3 Comments

Local variables are NOT initialised in Java.
@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 ;)
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.