static与final关键字问题
package com.aaa; import java.util.ArrayList; import java.util.List; class Apple { private static long a; private static long b = a++; public long geta(){ return b;} } class Orange {} public class Test0128 { public static void main(String[] args) { List apples = new ArrayList(); for(int i=0;i<3;i++){ apples.add(new Apple()); } for(int i=0;i<apples.size();i++){ Apple apple = (Apple) apples.get(i); System.out.println(apple.geta()); } } }
输出结果为: 0,0,0
问题: 当把private static long b = a++;中static 改为final时,输出结果变为0,1,2
倚天杖
相关分类