public int a =1;
public static int b =1;
public void a(){};
public static void b(){};
public class a{
int d = b ; //Syntax error on token "b", VariableDeclaratorId expected after this token
//MainActivity activity = new MainActivity();
//int a = activity.a;
int c = a; //访问外部类是有效的
}
public static class b{
MainActivity.b = 2 ; //Syntax error on token "b", VariableDeclaratorId expected after this token
} //修改外部类变量报错。只能访问。
//实例方法可以调用非静态成员,静态成员。
void c(){
a = 5;
b = a +5;
a();
b();
}
//静态方法可以调用非静态成员,静态成员。
static void d(){
a = 5; //Cannot make a static reference to the non-static field a
b = a +5; //Cannot make a static reference to the non-static field a
a(); //Cannot make a static reference to the non-static method a() from the type MainActivity
b();
}
//非静态外部类handler 调用非静态成员,静态成员。
Handler mhandler = new Handler(){
@Override
public void handleMessage(Message msg) {
a = 5;
b = a +5;
a();
b();
}
};
//静态外部类handler 调用非静态成员,静态成员。
static Handler mhandler2 = new Handler(){
@Override
public void handleMessage(Message msg) {
a = 5; //Cannot make a static reference to the non-static field a
b = a +5; //Cannot make a static reference to the non-static field a
a(); //Cannot make a static reference to the non-static method a() from the type MainActivity
b();
}
};
总结:
1.外部类 handler 访问也是非静态声明就能访问(非)静态属性