I am able to assign value to a static variable but am not able to print it out in the same static block.
If I move the static variable above the static block, then all works well. Now I am failing to follow the sequence of execution of the code. The code was run in java.
class ExampleStatic{
static {
cokePrice=12;
System.out.println("Coke Price is: R"+cokePrice);
}
static int cokePrice;
public static void main(String[] args) {
}
}
I expected the output to print the Coke Price is: R12. However an error says: Cannot reference a field before it is defined.