Enhanced for loop compiling fine for JDK 8 but not 7

后端 未结 6 1079
Happy的楠姐
Happy的楠姐 2021-01-03 18:36

Consider the following code snippet, I stumpled upon after some refactoring, when checkin why the build server reported a broken build but it was fine in my IDE:

<         


        
6条回答
  •  滥情空心
    2021-01-03 19:14

    I would say it is a compiler bug in the particular version of the Java 7 compiler that you are using.

    The earlier text is a field, and it is legal for the text local declared in the for statement to shadow a field.

    Then we look at what the for loop means. According to the JLS,

        for (String text : text) {...}
    

    is equivalent to

        for (Iterator #i = text.iterator(); #i.hasNext(); ) {
            String text = (String) #i.next();
            ...
        }
    

    As you can see the inner text is not in-scope for the text.iterator() expression.


    I tried searching the Oracle Java Bugs Database, but couldn't find anything that matched this scenario.

提交回复
热议问题