首页 新闻 赞助 找找看

这几行段段代码为何编译失败?

0
[待解决问题]

public class QQ {

public int aMethod() {
static int i = 0;
i++;
return i;
}

public static void main(String args[]) {
QQ test = new QQ();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}

}

梁山好汉的楷模的主页 梁山好汉的楷模 | 菜鸟二级 | 园豆:202
提问于:2018-02-23 14:23
< >
分享
所有回答(4)
0

static int i = 0;

改成 int i = 0;

悟行 | 园豆:12559 (专家六级) | 2018-02-23 15:08
0

在那个QQ类中的static int i = 0;去掉前面的static 

因为static的属性在一开始加载的时候就存在了,所以static属性不能放在非static 方法中

你去看看static 的说明就知道了

123爱迪生发松岛枫 | 园豆:256 (菜鸟二级) | 2018-02-23 15:16
0

static的意思,搞明白,问题就出来了

_Vegetables | 园豆:586 (小虾三级) | 2018-02-24 10:57
0

static只能用于静态类

偶像之路 | 园豆:3 (初学一级) | 2018-02-24 14:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册