简体   繁体   English

如果我不断收到错误怎么办:在封闭作用域中定义的局部变量 j 必须是最终的或有效的最终

[英]What to do if i keep getting error: Local variable j defined in an enclosing scope must be final or effectively final

i am trying to read how many rows are there on the location (i) and display separated by enter which one is displayed at the moment(j) - example 1 of 15 when pressed enter will display 2 of 15 here's part of code which causes error:我正在尝试读取位置 (i) 上有多少行,并通过 Enter 分隔显示当前显示的行 (j) - 按下 Enter 时 15 个示例中的 1 个将显示 15 个中的 2 个,这是导致代码的一部分错误:

int i = 0;
        int j = 0;


        try {
            ResultSet rs = stmt.executeQuery("SELECT * FROM product WHERE Location = 'REC1U'");
            while(rs.next()) {
                i++;
            }
            rs.next();
            tfield.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                        j++; // <--- this is causing above error
                        String l = String.valueOf(j);

                        count.setText(l);
                    }
                }

                });
        } finally {
            stmt.close();
        }

i have tried to make it final just to try to get rid of it but it wouldn't work anyways...我试图让它成为最终的只是为了摆脱它,但无论如何它都行不通......

You can use an object that has a counter inside which is final.您可以使用内部具有最终计数器的对象。 For example:例如:

final AtomicInteger j = new AtomicInteger(0);

//...
public void keyPressed(KeyEvent e) {
    j.incrementAndGet();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在封闭作用域中定义的局部变量必须是最终的或有效的最终变量 - Local variable i defined in an enclosing scope must be final or effectively final 如何解决错误:“我在封闭 scope 中定义的局部变量必须是最终的或有效的最终”? - How can I work around the error: “Local variable i defined in an enclosing scope must be final or effectively final”? 在封闭作用域中定义的局部变量必须是最终的或有效的最终变量 - Local variable defined in an enclosing scope must be final or effectively final 在封闭 scope 中定义的局部变量迭代必须是最终的或有效的最终 - local variable iteration defined in an enclosing scope must be final or effectively final 在封闭 scope 中定义的局部变量 ObjList 必须是最终的或有效的最终 - Local variable ObjList defined in an enclosing scope must be final or effectively final 在封闭范围内定义的局部变量 log 必须是 final 或有效 final - Local variable log defined in an enclosing scope must be final or effectively final 线程:封闭范围中定义的局部变量必须是final或有效的final - Threads: Local variable defined in an enclosing scope must be final or effectively final 在封闭的 scope 中定义的局部变量 collect 必须是最终的或有效的最终的 - Local variable collect defined in an enclosing scope must be final or effectively final 计时器错误的int值:在封闭范围内定义的局部变量ans必须是final或有效的final - int for timer error: Local variable ans defined in an enclosing scope must be final or effectively final Java 8 显示此错误。 在封闭作用域中定义的局部变量 itemList 必须是 final 或有效 final - Java 8 shows this error. Local variable itemList defined in an enclosing scope must be final or effectively final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM