'Why doesn't "return x.a;" signal an error? [duplicate]
I understand why "return a;" signals an error (because 'a' is non-static attribute and I'm trying to refer to it in a static method), but I don't understand why "return x.a;" doesn't signal an error. The 'a' attribute of 'x' is still non-static, shouldn't it be an error?
class A{
private int a=0;
private static int b =0;
public int m(){
return a;
}
public static int n(A x){
return x.a;//not an error
return a;//error
}
}
Solution 1:[1]
but I don't understand why "return x.a;" doesn't signal an error
return x.a is valid because x is accessible as a local variable inside the static method and is an instance of A. a is an instance property on the instance of A so x.a is perfectly valid.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lino |
