标签:
1 package fengke.finalandstatic; 2 /** 3 * final与static的区别 4 * @author 锋客 5 * 通过生成随机数的方式测试final与static的区别 6 * 结果: 7 * static生成一次,在资源被调用时; 8 * final确保值不变,每次被调用时都被生成; 9 * 10 */ 11 12 public class FinalTest { 13 public static void main(String[] args) { 14 MyClass myClass1 = new MyClass(); 15 MyClass myClass2 = new MyClass(); 16 System.out.println("myclass1的final"+myClass1.i); 17 System.out.println("myclass1的static"+myClass1.j); 18 System.out.println("myclass2的final"+myClass2.i); 19 System.out.println("myclass2的static"+myClass2.j); 20 21 } 22 23 } 24 25 class MyClass { 26 public final double i = Math.random(); 27 public static double j = Math.random(); 28 29 public MyClass() { 30 31 } 32 33 }
标签:
原文地址:http://www.cnblogs.com/fengke/p/4800273.html