从内部类引用的局部变量必须是最终的或有效的最终 [英] Local variables referred from inner class must be final or effectively final

查看:146
本文介绍了从内部类引用的局部变量必须是最终的或有效的最终的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看以下代码

private void displayInitialRevenue_Method() {
    //Get the dates from the combo
    String selectedCouple = revenueYearCombo.getSelectedItem().toString();
    if (selectedCouple.equals("Select Year")) {
        return;
    }
    String[] split = selectedCouple.split("/");
    //Related to DB
    double totalAmount = 0.0;
    //Get data from the database
    dbConnector = new DBHandler();
    dbConnector.makeConnection();
    DefaultTableModel model = (DefaultTableModel) initialRevenueTable.getModel();
    model.setRowCount(0);
    ResultSet selectAllDetails = dbConnector.selectAllDetails("SQL CODE ");
    try {
        if (selectAllDetails.isBeforeFirst() == false) {
            JOptionPane.showMessageDialog(null, "This table is empty");
        } else {
            while (selectAllDetails.next()) {
                String clientName = selectAllDetails.getString("Client Name");
                String providerName = selectAllDetails.getString("Provider Name");
                Double amountInvested = selectAllDetails.getDouble("Invest_Amount");
                //Update the table
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        Object[] row = {clientName, providerName, amountInvested};
                        model.addRow(row);
                    }
                });
                //Get the total
                totalAmount = totalAmount + amountInvested;
            }
            //Add the sum
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Object[] blankRow = {null};
                    model.addRow(blankRow);
                    Object[] row = {totalAmount};
                    model.addRow(row);

                }
            });
        }
    } catch (SQLException sql) {
        JOptionPane.showMessageDialog(null, sql.getLocalizedMessage());
    }
}

这整个方法在另一个线程内运行。 UI更新在 SwingUtilities.InvokeLater()中运行。不过请注意以下内容。

This entire method is running inside another thread. The UI updates are running inside SwingUtilities.InvokeLater(). However pls put attention to the below.

Object[]row = {totalAmount};
model.addRow(row);

变量 totalAmount 不是最终的,它是不能是最终的,因为它需要在上面的 while循环中计算。由于它不是 final 我无法在 SwingUtilities.InvokeLater()中使用它,因为错误显示从内部类引用的局部变量必须是最终的或有效的最终

The variable totalAmount is not final and it cannot be final because it need to be calculated in the above while loop. Since it is not final I am unable to use it inside the SwingUtilities.InvokeLater() as the error says Local variables referred from inner class must be final or effectively final

推荐答案

你可以制作一个 最终的附加变量,然后将计算的最终结果复制到其中。

You can make an additional variable that is Final and then copy the final result of the calculation into it.

while (selectAllDetails.next()) {
            String clientName = selectAllDetails.getString("Client Name");
            String providerName = selectAllDetails.getString("Provider Name");
            Double amountInvested = selectAllDetails.getDouble("Invest_Amount");
            //Update the table
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Object[] row = {clientName, providerName, amountInvested};
                    model.addRow(row);
                }
            });
            //Get the total
            totalAmount = totalAmount + amountInvested;
        }
final Double totalAmountInvested = totalAmount; // and now use this one in the Inner

这篇关于从内部类引用的局部变量必须是最终的或有效的最终的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆