Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem in compile time when using "var" in jdk 11 [duplicate]

when I use the word "var" the IDE recognize the command, but when I compile the code, it gives me an error:

Error:(10, 17) java: cannot find symbol

symbol: class var

location: class Exp

the code:

public final class Exp
{
    public static void main(final String[] args)
    {
        var x=5;
    }
}

So why does it happen? How can i solve it?

the pics: the project SDK is 11 and so is the language level

The module language level is 11 also

The full warning

like image 542
Roy Ash Avatar asked Nov 04 '18 12:11

Roy Ash


People also ask

Why VAR is not working in Java?

The reason may be you are using an older version of java. In Java 10, the var keyword has been introduced. e.g. instead of doing String str = "Java" , you can now just say var str = "Java" .

Is it good practice to use VAR in Java?

Using var can make code more concise without sacrificing readability, and in some cases it can improve readability by removing redundancy.

What is the use of var keyword in Java 11?

Java 11 allows to use var in a lambda expression and it can be used to apply modifiers to local variables.

What is the advantage of using VAR in Java?

In Java 10, the var keyword allows local variable type inference, which means the type for the local variable will be inferred by the compiler, so you don't need to declare that.


1 Answers

Thanks to @Marv the solution was

Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler

and raise the Project Bytecode version to 11. (I raised it from 8 to 11)

Pic: Go to Project bytecode version and raise it to max level

like image 110
Roy Ash Avatar answered Oct 16 '22 16:10

Roy Ash