【发布时间】:2018-04-08 11:19:35
【问题描述】:
public class Test {
class Foo {
int[] arr;
Foo(int[] userInput) {
arr = userInput;
}
}
public static void main(String[] args) {
int[] userInput = new int[]{1,2,3,4,5};
Foo instance = new Foo(userInput);
}
}
它给了我一个错误
error: non-static variable this cannot be referenced from a static context
我已经搜索了一些答案,但无法解决。
下面是我对这段代码的看法,我把userInput看成一个指针,编译器分配了5个int内存并给userInput分配了一个address,然后我传递这个地址(我知道java是@987654326 @) 到类Foo 构造函数,我认为instance 字段arr 得到了地址值。
这就是我的理解,我错了吗?
【问题讨论】: