Local Variable Type Inference not being recognized

好久不见. 提交于 2019-12-10 11:06:34

问题


I installed JDK 10 to try out the new features, and I got a big hung up on var - for some reason, even though the JDK was added to IntelliJ (version 2018.1), the following code still won't compile, saying Java cannot find the symbol var:

public class Variations {
    public static void main(String[] args) {
        var local = "foo";
        System.out.println(local);
    }
}

Am I missing something obvious here, or is there an option in IntelliJ I should enable?

EDIT: Both project and module SDK and Language Level are set to the Java 10 installation and lvl. 10 (though not the X - experimental level).


回答1:


Apparently, though a hard restart of the IDE is not enough, by setting language level to X - Experimental features on both the project and the module once, then re-setting it to lvl. 10 was enough to bring the IDE back in line.

I guess I should credit @CrazyCoder here for giving me the idea, even if indirectly.




回答2:


I searched on the web for a while and I found out that IntelliJ fully supports it. Have you checked if the SDK version you are running on IntelliJ is the 10th? You can check it by going to Project Structure (Alt+Shift+Ctrl+S), SDKS. From here if you don't see the label 10.0 in the left side of the panel just manually add it with the plus button. Hope this helps.




回答3:


In Settings/Build/Compiler/Java Compiler you must change the "Target bytecode version" to 10 in each one of your modules.




回答4:


Your code works fine with Java10 on my MacOS IntelliJ 2018.1 built on March 27 2018.

IntelliJ IDEA 2018.1 (Community Edition) Build #IC-181.4203.550, built on March 27, 2018

There might be error with either your IntelliJ or JDK. Try to use jshell from terminal instead. If can, should update your IntelliJ. If cannot, should check your installed JDK. The version on my machine:

"10" 2018-03-20 Java(TM) SE Runtime Environment 18.3 (build 10+46)



来源:https://stackoverflow.com/questions/49581032/local-variable-type-inference-not-being-recognized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!