Can i declare a static variable inside static memb

2019-03-14 04:49发布

private static int Fibonoci(int n) {
static int first=0;
static int second=1;
static int sum;
if(n>0)

i am getting a error "Illegal Modifier" and if i remove static keyword there is no error and i need those variables to be static

9条回答
三岁会撩人
2楼-- · 2019-03-14 05:09

You can not declare varibale as static inside a method. In otherwords we can say that, Local variables cannot be declared static.

查看更多
看我几分像从前
3楼-- · 2019-03-14 05:15

You have to define static variables as members in class. Variables those are defined within method are local variables and their lifecycles ends at the end of the method. local variables are call specific, member variables are object specific and static variables are class specific variables.

查看更多
Animai°情兽
4楼-- · 2019-03-14 05:15

You need to declare the static variables outside of the function:

static int first=0;
static int second=1;
static int sum;
private static int Fibonoci(int n) {
    if(n>0)
查看更多
登录 后发表回答