[ACCEPTED]-In Java, are variables declared inside static methods themselves static?-static
No, it's not a static variable. It's a local 6 variable. Any variable declared in a method 5 is a local variable. If you want a static 4 variable, you have to declare it outside 3 the method:
private static int parsedUntil = 0;
There's no way of declaring a 2 static variable which can only be used within 1 a single method.
no, A()
is a static method, and parsedUntil
is a local 10 variable inside A.
Modifiers like static
are not 9 valid in local variables (only final
is permitted 8 afaik)
Follow-up question: I read that a static 7 variable will only be initialized once.
true
Does 6 that mean the first time I call function 5 A() the value will be set to zero, but 4 every other time I call A(), that row 3 is omitted?
since parsedUntil is not a 2 static field, but a local variable in a 1 static method, this is not the case.
static
variables cannot be declared locally inside 3 methods - they can only be members of a 2 class, and they get initialised when the 1 class is loaded.
Java does not have static local variables 4 like C or C++ does, so you can never have 3 static int parsedUtil = 0;
.
So no, parsedUtil
is not in any sense "static". Its 2 value is initialised to 0 every time the 1 method is executed.
No it's not C.
parsedUntil is not static. It's 5 just a local variable. You cannot declare 4 static variable inside the method.
Regarding 3 second question - static variables can be 2 assigned as many times as you want. You 1 cannot reassign only final variables.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.