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);
}
}
It gave me an error
error: non-static variable this cannot be referenced from a static context
I already have searched some answers but cannot cannot solve it.
Here's how I think about this code, I view userInput
as a pointer, and the compiler allocate five int
memory and assign userInput with a address
, then I transfer this address (I know java is pass by value
) to class Foo
constructor, and I think instance
field arr
got the address value.
That's how I understand, am I wrong?