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
}
}
nmay be static but it's being given an instancexwhich does have the non-static attributes.return ais an error because that's not anint, nothing to do with static or not.arepresent? do you mean theint afield?ais notstaticso you can't access it in a static context.xis provided as an argument so you can access its fields via the instancestaticmeans?x.ais the field of the instancex. It works because it's a non-static field used with an instance (x). Inreturn a,ais a non-static field, but accessed from a static context (the methodn).