Java基础之Static关键字知识点

一 附上Demo,注释包含了 Static 与 this 相关知识点。
package com.dong.four;

public class TestP2 {

	public static void main(String[] args) {
		//测试下short数据类型
		short s1=1;
		//s1=(short) (s1+(short)1);//进行数据类型转换
		
		//s1=s1+1; 如果是这样的写法,会报错 必须要进行强制转换类型,并且对运算后的结果也需要进行转换
		
		//s1 +=1; 这样的计算后的结果是没问题的
		System.out.println(s1);
		
		int a = s1 + 1;//这样也不会报错
		System.out.println(a);

	}

}

二 附上Demo2,注释包含了this关键字相关知识点

package com.dong.four;

public class TestThis {

	private int a ;
	private int b ;
	private int c;
	public TestThis(){
		
	}
	
	public TestThis(int a,int b){
		this.a = a; //此处加上this关键字就是区分成员变量和局部变量 this关键字第一个用处
		this.b = b;
	}
	
	public TestThis(int a,int b,int c){
		/*this.a=a;  此时如果不想这样写 可以按照下面的方式写  this(a,b);
		this.b=b;*/
		this(a,b);//这个就相当于调用上面的构造方法      用处就是构造方法的调用
		this.c =c; //除此之外 this关键字是不能用在 static关键字里面
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值