Why in the below code is assigning a value to the static variable acceptable but using that same variable is not?
class Test
{
static
{
var=2; //There is no error in this line
System.out.println(var); //Why is there an error on this line if no error on the above line
}
static int var;
}
Because the usage is not on the left hand side of an assignment, as explained below:
From section 8.3.2.3 of the JLS, Restrictions on the use of Fields during Initialization: