As indicated in other answers, you should set your initial value as follows:
private static String foo = "initial value";
In addition, if you want to access this variable from anywhere, you need to reference it in a static context, for example:
Foo.foo
where Foo is the name of the class and Foo is the name of the variable.
This is really very useful for understanding the concept of static variables. Instead of referencing Foo as a member of some instance of the Foo class, you are referring to Foo as a member of the class itself. Thus, for all instances of Foo value of Foo will be the same since it belongs to the class , not an instance .
Inside a Foo class, you can avoid just calling Foo without qualifying it with the class name.
Luke m willis
source share