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
When you're doing this at class level, you're allowed to combine declaration and assignment in just one statement, like:
class A {
int a = 5;
}
Otherwise, you have to wrap the assignment withing a block (constructor, method, initializer block). For example:
class A {
int a;
public A() { a = 5; } //via constructor
void setA() { a = 5; } //via method
{ a = 5; } //via initializer block
}