I am new to Java and trying to wrap my head around the difference between defining a variable as static, and including a variable within a static block. I have formulated a simplified example to illustrate my confusion:
import java.util.Properties;
public class Foo {
static Properties myProps = new Properties();
static {
Properties myOtherProps = new Properties();
}
public static void bar() {
Foo.myProps.forEach(myProps::put); // Works just fine
Foo.myOtherProps.forEach(myProps::put); // Throws cannot find symbol error
}
}
The static variable myProps
seems to be referenced normally by the bar()
method while myOtherProps
can't be referenced from that part of the code.
What is the difference here between wrapping code in a static code block vs. defining a variable as static?