java(非)静态方法,(非)静态外部类访问静态属性总结

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 访问也是非静态声明就能访问(非)静态属性

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值