站点图标 度崩网-几度崩溃

final与static的区别

static:

final:

final 和 static 修饰成员变量加载过程例子

import java.util.Random;
 
public class TestStaticFinal {
 
	public static void main(String[] args) {
		StaticFinal sf1 = new StaticFinal();
		StaticFinal sf2 = new StaticFinal();
		
		System.out.println(sf1.fValue == sf2.fValue);//打印false
		System.out.println(sf1.sValue == sf2.sValue);//打印true
	}
}
 
class StaticFinal {
	
	final int fValue = new Random().nextInt();
	static int sValue = new Random().nextInt();
	
}