9

如何userId在此处的匿名内部子类中获取传递给此方法的值?

public void doStuff(String userID) {
    doOtherStuff(userID, new SuccessDelegate() {
        @Override
        public void onSuccess() {
            Log.e(TAG, "Called delegate!!!! "+ userID);
        }
    });
}

我收到此错误:

不能在不同方法中定义的内部类中引用非最终变量 userID

我很确定我不能将它分配为 final,因为它是一个具有未知值的变量。我听说这种语法确实以某种方式保留了范围,所以我认为一定有一个我还不知道的语法技巧。

4

5 回答 5

10

正如这里的其他人所说,局部变量必须是 final 才能被内部类访问。

这是(基本上)为什么会这样......如果您编写以下代码(长答案,但在底部,您可以获得简短版本:-):

class Main
{
    private static interface Foo
    {
        void bar();
    }

    public static void main(String[] args)
    {
        final int x;
        Foo foo;

        x = 42;
        foo = new Foo()
        {
            public void bar()
            {
                System.out.println(x);
            }
        };

        foo.bar();
    }
}

编译器大致这样翻译:

class Main
{
    private static interface Foo
    {
        void bar();
    }

    public static void main(String[] args)
    {
        final int x;
        Foo foo;

        x = 42;

        class $1
            implements Foo
        {
            public void bar()
            {
                System.out.println(x);
            }
        }

        foo = new $1();
        foo.bar();
    }
}

然后这个:

class Main
{
    private static interface Foo
    {
        void bar();
    }

    public static void main(String[] args)
    {
        final int x;
        Foo foo;

        x = 42;
        foo = new $1(x);
        foo.bar();
    }

    private static class $1
        implements Foo
    {
        private final int x;

        $1(int val)
        {
           x = val;
        }

        public void bar()
        {
            System.out.println(x);
        }
    }
}

最后是这个:

class Main
{
    public static void main(String[] args) 
    {
        final int x;
        Main$Foo foo;

        x = 42;
        foo = new Main$1(x);
        foo.bar();
    }
}

interface Main$Foo
{
    void bar();
}

class Main$1
    implements Main$Foo
{
    private final int x;

    Main$1(int val)
    {
       x = val;
    }

    public void bar()
    {
        System.out.println(x);
    }
}

重要的是将构造函数添加到 $1 的位置。想象一下,如果你能做到这一点:

class Main
{
    private static interface Foo
    {
        void bar();
    }

    public static void main(String[] args)
    {
        int x;
        Foo foo;

        x = 42;
        foo = new Foo()
        {
            public void bar()
            {
                System.out.println(x);
            }
        };

        x = 1;

        foo.bar();
    }
}

您会期望 foo.bar() 会打印出 1,但实际上会打印出 42。通过要求局部变量是最终的,这种令人困惑的情况不会出现。

于 2010-07-14T22:46:30.323 回答
7

当然,您可以将其指定为 final - 只需将该关键字放在参数的声明中:

public void doStuff(final String userID) {
   ...

我不确定您将其作为具有未知值的变量是什么意思;最终的所有意思是,一旦将值分配给变量,就不能重新分配它。由于您没有在方法中更改 userID 的值,因此在这种情况下将其设为 final 没有问题。

于 2010-07-14T22:25:12.137 回答
2

在 Java 8 中,这发生了一些变化。您现在可以访问实际上是final的变量。Oracle 文档中的相关片段和示例(重点是我的):

但是,从 Java SE 8 开始,本地类可以访问封闭块的本地变量和参数,它们是 final 或 实际上是 final

有效最终: 一个非最终变量或参数,其值在初始化后从未更改,实际上是最终的

例如,假设变量numberLength未声明为 final,并且您在PhoneNumber 构造函数中添加了突出显示的赋值语句:

PhoneNumber(String phoneNumber) {
    numberLength = 7; // From Kobit: this would be the highlighted line
    String currentNumber = phoneNumber.replaceAll(
        regularExpression, "");
    if (currentNumber.length() == numberLength)
        formattedPhoneNumber = currentNumber;
    else
        formattedPhoneNumber = null;
}

由于这个赋值语句,变量numberLength不再是有效的最终变量。结果,Java 编译器生成类似于“从内部类引用的局部变量必须是最终或有效最终”的错误消息,其中内部类 PhoneNumber尝试访问该numberLength变量:

if (currentNumber.length() == numberLength)

从 Java SE 8 开始,如果在方法中声明本地类,它可以访问方法的参数。例如,您可以在 PhoneNumber 本地类中定义以下方法:

public void printOriginalNumbers() {
    System.out.println("Original numbers are " + phoneNumber1 +
        " and " + phoneNumber2);
}

方法printOriginalNumbers访问参数 phoneNumber1phoneNumber2方法validatePhoneNumber

于 2015-01-19T01:03:11.847 回答
1

final制作它有什么问题

public void doStuff (final String userID)
于 2010-07-14T22:26:00.870 回答
1

声明方法

public void doStuff(final String userID)

该值需要是最终的,以便编译器可以确保它不会改变。这意味着编译器可以随时将值绑定到内部类,而无需担心更新。

您的代码中的值没有改变,所以这是一个安全的改变。

于 2010-07-14T22:26:16.097 回答