Reputation: 3
I declared var type variable however the program throws compilation error. Can someone suggest the reason for this error?
var avg2 = 10.0;
Error:
javac "VarTypeVariables.java" (in directory: S:\29032020 Java\12 Var Type Variables)
VarTypeVariables.java:12: error: cannot find symbol
var avg2 = 10.0; //type based on 10.0 (i.e double)
^
symbol: class var
location: class VarTypeVariables
1 error
Compilation failed.
Code:
class VarTypeVariables {
public static void main (String args[]){
var avg2 = 10.0;
}
}
Upvotes: 0
Views: 6527
Reputation: 61
I get the same error using java 17. I change java version on spring boot project in the same moment the project was running. And after this state showed up.
To fix this, change java version again e clean your project with > mvn clean, after reload the project and its done!
Upvotes: 0
Reputation: 1
I already had the Java 17 version in use and it still didn't work. So I made the settings below and it worked.
Add to pom.xml:
<maven.compiler.release>17</maven.compiler.release>
<java.version>17</java.version>
Go to: Settings > Build, Execution, Deployment > Compiler > Java Compiler
Upvotes: 0
Reputation: 7335
The output from the java -version command
you posted above indicates you are using Java 8. var
was only introduced in to the language at java 10.
If you want to use var
you need to install a more recent version of Java - Java 10 or above.
Upvotes: 2
Reputation: 901
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"
.
Upvotes: 0
Reputation: 3304
var keyword was added in java 10
for type inference. You have to upgrade to this version of java
to make this example working.
Upvotes: 0