[java]代码库
package fengke.finalandstatic;
/**
* final与static的区别
* @author 锋客
* 通过生成随机数的方式测试final与static的区别
* 结果:
* static生成一次,在资源被调用时;
* final确保值不变,每次被调用时都被生成;
*
*/
public class FinalTest {
public static void main(String[] args) {
MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();
System.out.println("myclass1的final"+myClass1.i);
System.out.println("myclass1的static"+myClass1.j);
System.out.println("myclass2的final"+myClass2.i);
System.out.println("myclass2的static"+myClass2.j);
}
}
class MyClass {
public final double i = Math.random();
public static double j = Math.random();
public MyClass() {
}
}