JAVA 基础

package 第六章.第六章练习;

class SelfCounter {
	  private static int count = 0;
	  private int id = count++;
	  public SelfCounter(String type){
		  System.out.println(count+":count的值");
		  System.out.println(type+":type");//判断是先执行了谁
		  System.out.println(id+":id的值");
	  }
	  public String toString() {
	    return "SelfCounter " + id;
	  }
	}

	class WithFinalFields {
	  final SelfCounter scf = new SelfCounter("scf");
	  static final SelfCounter scsf = new SelfCounter("scsf"); //static 只初始化一次 而且是最先初始化的 不会再执行第二次了
	  public String toString() {
	    return "scf = " + scf + "\n"+
	    	   "scsf = " + scsf;
	  }
	}

	public class Test19 {
	  public static void main(String args[]) {
	    System.out.println("First object:");
	    System.out.println(new WithFinalFields()+"\n");
	    System.out.println("Second object:");
	    System.out.println(new WithFinalFields());
	  }
	} 

/**
 * 执行后的结果
 *  First object:
	1:count的值
	scsf:type
	0:id的值
	2:count的值
	scf:type
	1:id的值
	scf = SelfCounter 1
	scsf = SelfCounter 0
	
	Second object:
	3:count的值
	scf:type
	2:id的值
	scf = SelfCounter 2
	scsf = SelfCounter 0
 */
	
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值