class Foo {
public Foo() { }
}
class Bar {
static Foo foo = new Foo(); // This is legal...
public static void main(String[] args) {
static int
I suppose that your expectation was behaviour like a static
local variable with an initializer in a C function, which is initialized only once, namely in the first call of the function, but not in subsequent calls.
Java static
fields is similar to C static
local variables in that static fields are initialized only once, namely when the class is loaded.
Regarding the other answers mentioning it, final
has nothing to do with this!
final
means that the value of a variable/field cannot be changed after it has been initialized.
The idiomatic solution in Java is not using static
fields in the first place, except for constants and things like caches and registries.
Often, a better solution is to write a new class instead that holds the value you would wanted to keep in the "static
" variable as a regular (non-static) field. You can initialize it in the field declaration or in the new class's constructor, and then do the computation you wanted to do in a (non-static) method.
Declare the variable as final, not as static.
Static means that there is one per class not one per instance of the class. Final means it can't be modified after creation. (Although note that making a reference final does not make the class it references immutable).
In other words if you have a
final String[] array = new String[3];
You can no longer change that variable, for example if you wanted to assign to it a new array with a different size you could not. However you can modify the contents of the array.
array[0] = "test";
Because this modifies the contents, not the array itself.
The same thing holds for any mutable objects.
Java doesn't allow user to create static variable locally as in java static variable's scope is global(i.e. throughout the program ) so there is no point of creating static variable locally thus in order to avoid ambiguousness java doesn't allow user to create static variable locally. We only can create static variable as global static variable .
Other people have explained how to deal with this at the coding level. Allow me to explain the logical and philosophical reasons why static within a method makes no sense. You have to ask the question "How long do you want the variable to last?".
So how long do you want your 'static within a method' variable to last? If it's until the end of the method, then you can just use it without static. If it's for the lifetime of the class, then you can declare it as a static member variable. What other options are there?
C++ allows statics within a method, but they end up behaving just like a static class variable, but with reduced scope. Even in C++ they are rarely used. They also end up being stored exactly like static member variables. The pattern is widely considered to be dangerous and confusing, because it mounts to having a method 'remember' the value of what looks like a local variable from one invocation to another - a value which would be changed if some other piece of code chooses to execute the method in between the two invocations.
The Java designers decided that the small amount of benefit wasn't worth the additional complexity of the language.
You have to make the static final static
or remove static
.
In Java, static means that it's a variable/method of a class, it belongs to the whole class but not to one of its certain objects. This means that static keyword can be used only in a 'class scope'.
Generally, in C, you can have statically allocated locally scoped variables. Unfortunately this is not directly supported in Java. But you can achieve the same effect by using nested classes.
For example, the following is allowed but it is bad engineering, because the scope of x is much larger than it needs to be. Also there is a non-obvious dependency between two members (x and getNextValue).
static int x = 42;
public static int getNextValue() {
return ++x;
}
One would really like to do the following, but it is not legal:
public static int getNextValue() {
static int x = 42; // not legal :-(
return ++x;
}
However you could do this instead,
public static class getNext {
static int x = 42;
public static int value() {
return ++x;
}
}
It is better engineering at the expense of some ugliness.
The variables with in a method are local variables and their scope lies within the method and they get destroyed after the method execution. i.e. you cannot use a local variable outside the current method which contradicts with the definition of class/static variable. Therefore, declaring a static variable inside a method makes no sense, if you still try to do so, a compile time error will be generated i.e (illegal start of expression ^static int a = 0)