最终局部变量 dc 无法赋值,因为它是在封闭类型中定义的

The final local variable dc cannot be assigned, since it is defined in an enclosing type

public static void createTable() {
    final DatabaseConnection dc;
    final Connection con;
    final String que;
    Statement state;
    new Thread(() -> {
        dc = new DatabaseConnection("check_table_exists");
        con = dc.con;
        que = "CREATE TABLE IF NOT EXISTS " + DatabaseConnection.TABLE + " (id INT(11) NOT NULL AUTO_INCREMENT,itemId INT(200), itemName VARCHAR(200), amount INT(200),uuid VARCHAR(200), timestamp BIGINT(200), PRIMARY KEY (id))";
        try {
            state = con.createStatement();
            state.execute(que);
            state.close();
            dc.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
            dc.close();
        }
    }).start();
}

我只是想知道如何解决这个问题。我还在 'state' 变量上收到错误 'Local variable state defined in an enclosing scope must be final or effectively final'。我相信这是因为我正在使用我发现的一些旧源代码,但是,我似乎找不到解决方案。

使用 Java,除了在声明中,您不能将任何内容分配给标记为 final 的变量。另一方面,您需要这些变量是最终的才能在此闭包中使用。这在 JS 之后可能会感到沮丧。所以你可能需要对这个特定的代码做的是将变量声明移到闭包中,删除 final 修饰符。如果您仍然需要从外部访问它们,您可能会定义一个实现 Runnable 接口的 class,并通过 get 方法使这些变量成为此 class 实例的字段。

此外,您真的需要这段代码异步 运行 吗?