class level error : expected in java

后端 未结 4 507
陌清茗
陌清茗 2021-01-29 13:36

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

4条回答
  •  感情败类
    2021-01-29 13:55

    You can not write code into the class, only in method, constructor or into initializer {} block. That is why you get syntax error. Probably you want to use initializer block like this:

    class my{
     int a;
     {
      a=1;
     }
    }
    

提交回复
热议问题