In Java are unmodified method variables, that lack the final
,
qualifier re-initialized each time in a
- static method
- instance method
If the answer to 1. or 2., (or both) would the final
qualifier
allow Java to perform an optimization and store the method
variable only once?
If the answer depends on the type of the variable, which type of
variables are optimized/unoptimized? For instance, is String
,
int
optimized while Map
not optimized?
For comparison, Java would only store a static class variable such as
private static final String foo = "Teenage Mutant Ninja Turtle";
once. To clarify: the question is whether or not
1:
static SomeReturnValueOrVoid SomeMethod() {
// 1.a Not modified, is this reinitialized each method call?
String foo = "Teenage Mutant Ninja Turtle";
// 1.b Marked final, is this reinitialized each method call?
final String bar = "Teenage Mutant Hero Turtle";
}
2:
SomeReturnValueOrVoid SomeMethod() { // not static
// 2.a Not modified, is this reinitialized each method call?
String foo = "Teenage Mutant Ninja Turtle";
// 2.b Marked final, is this reinitialized each method call?
final String bar = "Teenage Mutant Hero Turtle";
}
is equivalent to
3:
class SomeClass {
static final String foo = "Teenage Mutant Ninja Turtle";
SomeReturnValueOrVoid SomeMethod() {
// Uses foo
}
static SomeReturnValueOrVoid SomeMethod() {
// Uses foo
}
...
}