编辑: 我需要改变几个变量的值,因为他们通过计时器运行几次。我需要在计时器的每次迭代中不断更新值。我不能将值设置为final,因为这将阻止我更新值,但是我得到了我在下面的初始问题中描述的错误:

我之前写过如下内容:

我得到错误“不能引用在不同方法中定义的内部类中的非最终变量”。 这发生在名为price的double和名为priceObject的price上。你知道我为什么会有这个问题吗?我不明白为什么我要做最后申报。如果你能看到我在做什么,我要怎么做才能解决这个问题。

public static void main(String args[]) {

    int period = 2000;
    int delay = 2000;

    double lastPrice = 0;
    Price priceObject = new Price();
    double price = 0;

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            price = priceObject.getNextPrice(lastPrice);
            System.out.println();
            lastPrice = price;
        }
    }, delay, period);
}

当前回答

这是另一种解释。考虑下面这个例子

public class Outer{
     public static void main(String[] args){
         Outer o = new Outer();
         o.m1();        
         o=null;
     }
     public void m1(){
         //int x = 10;
         class Inner{
             Thread t = new Thread(new Runnable(){
                 public void run(){
                     for(int i=0;i<10;i++){
                         try{
                             Thread.sleep(2000);                            
                         }catch(InterruptedException e){
                             //handle InterruptedException e
                         }
                         System.out.println("Thread t running");                             
                     }
                 }
             });
         }
         new Inner().t.start();
         System.out.println("m1 Completes");
    }
}

这里的输出是

m1完成

线程运行

线程运行

线程运行

................

现在方法m1()完成,我们将引用变量o赋值为空,现在外部类对象有资格进行GC,但内部类对象仍然存在,与正在运行的线程对象有(has - a)关系。没有外部类对象,就不可能存在m1()方法,没有m1()方法,就不可能存在它的局部变量,但如果内部类对象使用m1()方法的局部变量,那么一切都是不言自明的。

为了解决这个问题,我们必须创建一个局部变量的副本,然后必须用内部类对象将其复制到堆中,java只对final变量做了什么,因为它们实际上不是变量,它们像常量(一切都发生在编译时,而不是在运行时)。

其他回答

如果变量要求为final,那么你可以将变量的值分配给另一个变量,并使其为final,这样你就可以使用它。

我只是写了一些东西来处理作者的意图。 我发现最好的办法是让构造函数取所有的对象,然后在你实现的方法中使用构造函数对象。

但是,如果您正在编写一个泛型接口类,那么您必须传递一个Object,或者最好是一个Object列表。这可以通过Object[]完成,甚至更好,Object…因为这样更容易打电话。

请看下面的例子。

List<String> lst = new ArrayList<String>();
lst.add("1");
lst.add("2");        

SomeAbstractClass p = new SomeAbstractClass (lst, "another parameter", 20, true) {            

    public void perform( ) {                           
        ArrayList<String> lst = (ArrayList<String>)getArgs()[0];                        
    }

};

public abstract class SomeAbstractClass{    
    private Object[] args;

    public SomeAbstractClass(Object ... args) {
        this.args = args;           
    }      

    public abstract void perform();        

    public Object[] getArgs() {
        return args;
    }

}

请参阅这篇关于Java闭包的文章,它支持开箱即用: http://mseifed.blogspot.se/2012/09/closure-implementation-for-java-5-6-and.html

版本1支持通过自动转换传递非最终闭包: https://github.com/MSeifeddo/Closure-implementation-for-Java-5-6-and-7/blob/master/org/mo/closure/v1/Closure.java

    SortedSet<String> sortedNames = new TreeSet<String>();
    // NOTE! Instead of enforcing final, we pass it through the constructor
    eachLine(randomFile0, new V1<String>(sortedNames) {
        public void call(String line) {
            SortedSet<String> sortedNames = castFirst();  // Read contructor arg zero, and auto cast it
            sortedNames.add(extractName(line));
        }
    });

你能创建匿名内部类的lastPrice, priceObject和price字段吗?

我注意到一个没有提到的解决方案(除非我错过了,如果我错过了,请纠正我)是使用类变量。尝试在一个方法中运行一个新线程时遇到了这个问题:new thread (){Do Something}。

从下面调用doSomething()将工作。你不一定要声明它为final,只需要改变变量的作用域,这样它就不会在内部类之前被收集。当然,除非您的流程非常庞大,改变范围可能会产生某种冲突。我不想让我的变量成为final,因为它绝不是final/constant。

public class Test
{

    protected String var1;
    protected String var2;

    public void doSomething()
    {
        new Thread()
        {
            public void run()
            {
                System.out.println("In Thread variable 1: " + var1);
                System.out.println("In Thread variable 2: " + var2);
            }
        }.start();
    }

}

使用匿名类时,只能从包含类中访问最终变量。因此,您需要声明正在使用的变量为final(这对您来说不是一个选项,因为您正在更改lastPrice和price),或者不要使用匿名类。

因此,您的选择是创建一个实际的内部类,您可以在其中传递变量并以正常方式使用它们

or:

对于你的lastPrice和price变量,有一个快速(在我看来很难看)的方法,就是像这样声明它

final double lastPrice[1];
final double price[1];

在匿名类中,你可以这样设置值

price[0] = priceObject.getNextPrice(lastPrice[0]);
System.out.println();
lastPrice[0] = price[0];