How does this code work fine and prints 9?
public class Dog{
static {
age=9;
}
static int age=7;
}
And this code doesn't compile(illegal forward reference)? Notice I changed age in static block.
public class Dog{
static {
age++;
}
static int age=7;
}
Another question is how do both of them even work? From my prior Java knowledge I knew a rule that:
you can't access variable before declaring them
. So how does static block know what variable age actually is?