public class TestVariableDeclaration{
int j; // ERROR
j=45; // ERROR
static{
int k;
k=24;
}
{
int l;
l=25;
}
void local(){
int loc;
loc=55;
}
}
- In the above why can't I declare a variable "j" and then initialize directly under a class
- I can declare and then initialize in the same manner under a Method,Static/Instance initialization block?
- What makes the difference, I am aware about the fact that Java does not support Declaring and then initializing a instance variable. What's the reason behind that??
int j=45;you can initialize while declaringj=45;only declarations are allowed but not statements.