when I am coding this at class level
int a;
a=5;
it throws error : \"identifier expected\"
But when I declare it as a local variable li
The reason following:
int a=5;
declared at class level does not produce compile time error when:
void m1() {
int a;
a=5;
}
is declared because m1() has its own scope.
For instance, if don't declare and access variable a, it would refer to class's field, where as when you declare a locally, you'd always refer to one declared inside a.
PS : You cannot do following at class level:
int a;
a=5;
You'd have to:
int a=5;