java语法 逻辑 啥的

链接: https://www.nowcoder.com/questionTerminal/48b14664da4c450dae1316c98a5f47d3

来源:牛客网

  • 如下代码的输出结果是什么?
public class Test {
     public int aMethod(){
         static int i = 0 ;
         i++;
         return i;
     }
public static void main(String args[]){
     Test test = new Test();
     test.aMethod();
     int j = test.aMethod();
     System.out.println(j);
     }
}

【编译错误,static 静态变量只能定义在类主体(main)中,不能在方法中。因为static变量是在先于类的方法而加载的,在static变量加载的时候,方法还没有被分配内存,所以 static int i = 0;是错的,去掉static,可编译成功

Java中不允许定义静态局部变量。static不能修饰局部变量。不过static可以修饰类的成员变量,而不是放在方法里。

猜你喜欢

转载自blog.csdn.net/qq_33608638/article/details/79667790
今日推荐