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 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" .
Using var can make code more concise without sacrificing readability, and in some cases it can improve readability by removing redundancy.
Java 11 allows to use var in a lambda expression and it can be used to apply modifiers to local variables.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With