相关疑难解决方法(0)

局部变量范围的问题.怎么解决?

尝试statemet.executeUpdate()在我的代码中执行时,我收到以下错误:

Local variable statement defined in an enclosing scope must be final or effectively final.
Run Code Online (Sandbox Code Playgroud)

到目前为止这是我的代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class a1 {

    protected Shell shell;
    private Text text;
    private Text text_1;
    private Text text_2;
    private Text text_3;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try { …
Run Code Online (Sandbox Code Playgroud)

java scope

54
推荐指数
2
解决办法
15万
查看次数

可能尚未初始化局部变量

问题是关于"int amount".我正在尝试创建一个系统,每隔几秒就会将一个数量添加到起始量.

public static void main(String[] args) {

    final int WIDTH = 600;
    final int HEIGHT = 600;
    int amount = 0;

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            int amount = amount + 1;
        }
    }, 2*1000, 2*1000);

    JFrame frame = new JFrame("TimerTest");
    frame.setVisible(true);
    frame.setSize(WIDTH, HEIGHT);
    frame.setResizable(false);
    frame.setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = new JLabel("");
    label.setText(String.valueOf(amount));
    frame.add(label);
  }
}
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
71
查看次数

标签 统计

java ×2

scope ×1