如何避免使用FINAL? (Java)的

时间:2015-07-13 18:45:13

标签: java

我收到了下一个错误:Cannot refer to a non-final variable inside an inner class defined in a different method.

我不想使用FINAL ArrayList,因为我必须设置修改一些东西。

for (int i = 0; i < verOrd.size()-1; i++)
{
    final int p = i;
    instruccion  = new JLabel("Instrucción de Orden "+verOrd.get(i).getNumOrden());
    statusIns.add(no);
    statusIns.add(ok);
    ventana.add(instruccion);
    ventana.add(no);
    ventana.add(ok);
    ventana.add(sig2);

    int a = 0;

    sig2.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0) {

            if (ok.isSelected())

            {
                verOrd.get(p).setStatus("Finalizada");

            }                                   
        }

    });
}
for (int i = 0; i < verOrd.size()-1; i++)
{
    final int p = i;

    instruccion  = new JLabel("Instrucción de Orden "+verOrd.get(i).getNumOrden());
    statusIns.add(no);
    statusIns.add(ok);
    ventana.add(instruccion);
    ventana.add(no);
    ventana.add(ok);
    ventana.add(sig2);

    int a = 0;  

    sig2.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0) {

            if (ok.isSelected())

            {
                verOrd.get(p).setStatus("Finalizada");

            }                                   
        }

    });
}

3 个答案:

答案 0 :(得分:2)

您可以将arrayList设为“final”,并继续添加/删除该arrayList中的内容。

只有你不能做的事情是将arrayList变量分配给其他东西。

示例:

let paymentResultDic = completedPayment.confirmation as NSDictionary


        let dicResponse: AnyObject? = paymentResultDic.objectForKey("response")
        println(dicResponse!.objectForKey("create_time"))
        println(dicResponse!.objectForKey("id"))
        println(dicResponse!.objectForKey("intent"))
        println(dicResponse!.objectForKey("state"))

        let dicClient: AnyObject? = paymentResultDic.objectForKey("client")
        println(dicClient!.objectForKey("environment"))
        println(dicClient!.objectForKey("paypal_sdk_version"))
        println(dicClient!.objectForKey("platform"))
        println(dicClient!.objectForKey("product_name"))

        println(paymentResultDic.objectForKey("response_type"))

答案 1 :(得分:0)

您可以尝试封装变量(通过定义getter和setter)。这是OOP方法

答案 2 :(得分:0)

你可以在类中创建一个arraylist作为变量For。,

Class Foo{

private ArrayList<type> list;

public Foo(){
list = new ArrayList<>(); 
 }

//Now you can use the list in your methods without making it final
}