使用本地类时无效的构造函数引用? [英] Invalid constructor reference when using local class?

查看:145
本文介绍了使用本地类时无效的构造函数引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

package com.gmail.oksandum.test;

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
    }

    public void foo() {
        class LocalFoo {

            LocalFoo(String in) {
                //Some logic
            }

        }

        List<String> ls = new ArrayList<>();
        ls.stream().map(LocalFoo::new); //Line 21
    }

}

我的IDE给出了我没有错。也就是说,直到我尝试构建项目并运行它。当我这样做时,它给我一个编译器错误,如下所示:

my IDE gives me no errors. That is, until I try to build the project and run it. When I do that it gives me a compiler error that looks like this:

Error:(21, 24) java: incompatible types: cannot infer type-variable(s) R
    (argument mismatch; invalid constructor reference
      cannot access constructor LocalFoo(java.lang.String)
        an enclosing instance of type com.gmail.oksandum.test.Test is not in scope)

现在,我想,鉴于错误信息,这个如果foo()是静态的,就不会发生。而且非常正确,只有当foo()是一个实例方法时才会发生这种情况。并且只有当LocalFoo是实例方法中的本地类时才会发生,并且只有在使用构造函数引用时(即从不是常规方法引用)。

Now, I figured, given the error message, that this wouldn't happen if foo() were static. And quite right, this only happens if foo() is an instance method. And it only happens if LocalFoo is a local class in the instance method, and only if a constructor reference is used (i.e never a regular method reference).

更重要的是,如果我将第21行更改为

What's more, if I change line 21 into

ls.stream().map(str -> new LocalFoo(str));

编译器突然没有错误。

所以回顾一下。如果我尝试在实例方法中声明的本地类上使用构造函数引用,编译器会抱怨无法访问构造函数,我很困惑。

So to recap. If I try to use a constructor reference on a local class declared within an instance method, the compiler complains about not being able to access the constructor, about which I am confused.

如果有人能够阐明为什么会发生这种情况,我们将不胜感激。
谢谢。

If someone could shed some light on why this happens it would be appreciated. Thanks.

推荐答案

看起来 LocalFoo 被视为待遇不知何故,像一个非静态类。这就是为什么它声称没有测试的实例在范围内。

It looks like LocalFoo is treated somehow like a non-static class. That's why it claims no instance of Test is in scope.

从教程:


本地类是非静态的,因为它们可以访问封闭块的实例成员。因此,它们不能包含大多数静态声明。

Local classes are non-static because they have access to instance members of the enclosing block. Consequently, they cannot contain most kinds of static declarations.

https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html

方法 foo()或类 LocalFoo 必须是静态的才能工作。但是方法中的类不能声明为static。因此,如果 foo()应保持非静态(作为内部静态类),则必须将其移出方法。
另一种选择就是使用它:

ls.stream()。map(s - > new LocalFoo(s));

The method foo() or the class LocalFoo must be static for this to work. But a class inside a method can't be declared as static. So you'd have to move it out of the method if foo() should remain nonstatic (as an inner, static class). Another option is to just use this:
ls.stream().map(s -> new LocalFoo(s));

应该有一种方法可以说 Test.this.LocalFoo ,但这不起作用。如果确实如此,编译器也应该只接受 LocalFoo :: new

There should be a way to just say Test.this.LocalFoo, but that doesn't work. And if it did the compiler should also just accept LocalFoo::new.

现在有一个错误报告: https://bugs.openjdk.java.net/browse/JDK-8144673

(参见 Brian Goetz评论

There is a bug report now: https://bugs.openjdk.java.net/browse/JDK-8144673
(See comment by Brian Goetz)

这篇关于使用本地类时无效的构造函数引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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