当前位置: 代码迷 >> J2SE >> 高手来看一下 关于一个final的有关问题
  详细解决方案

高手来看一下 关于一个final的有关问题

热度:116   发布时间:2016-04-24 12:29:39.0
高手来看一下 关于一个final的问题
这个程序中CountInt类的中的b是final类型的为什么可以赋值一个可变的数。
Java code
import java.util.ArrayList;import java.util.List;public class FinalTest{    public static void main(String[] args)    {        FilledList<CoutInt> foo = new FilledList<CoutInt>(CoutInt.class);        System.out.println(foo.create(14));    }} class CoutInt{    private static int a;    private final int b = a++;    @Override    public String toString()    {        return Integer.toString(b);    }    }class FilledList<T> {    private Class<T> classtype;    public FilledList(Class<T> type)    {        this.classtype = type;    }    public List<T> create(int Elements)    {        List<T> result =  new ArrayList<T>();    try{        for(int i=0;i<Elements;i++)        {            result.add(classtype.newInstance());        }       }catch(Exception ex)       {           throw new RuntimeException();       }        return result;    }        }


------解决方案--------------------
b的值赋值后没有变
你每次new 不同的CoutInt ,不同的CoutInt的对象的b是不一样的 
但是CoutInt的对象的b的值赋值后就没有再改变了
  相关解决方案