Var key word in java not working

I’m in the classes section of the intermediate level of the Ultimate Java course and for some reason when I type in the code the same way that it comes up in the tutorial the compiler gives me an error at the var keyword. I’m pretty sure I’ve checked it’s the same, there’s not a lot of code put in so far so I can’t understand whats going wrong.

If you are in the “creating objects” video, it is 48 seconds in right where he types var. When I do it var comes up red and I get a “Cannot resolve symbol ‘var’” error.

Hi did you get it to work? It could be an issue with the version of java you are running or your configuration in your ide

1 Like

Hi, yea I did play about with the versions and settings until something worked by trial and error. However I am still getting more similar errors as I move through the course into the OOP Module. I am using language level 20 currently and I have an error as shown in my screen shot here.

I am a bit new to these configurations so forgive me if this doesn’t make sense in parts

Hi, you may need to check the project and modules sections in project structure to make sure your JDK version matches what there in intellij, check this link and see if that helps. You may also need to update your version of intellj

1 Like

Thanks, I’ve just been going into the modules section like you say and just adjusting the JDK from there until I get something that works.

I’m not sure I remember any part where he talks about issue like these. These videos are 4 years old like so it must be like you say with updating Intellij.

I don’t understand why you need all the different previous versions though. Wouldn’t it make sense just to have one up to date version?

I think its because some projects / companies use different versions and their codebase is dependant on that specific version.

Yea its weird, I never had those issues, I just made sure to download the latest version of JDK, JRE and intellj and it worked for me. I hope you don’t have other instances of those java programs installed on your system somewhere otherwise it could potentially cause clashes

1 Like

I downloaded 17 upwards, I thought maybe that was what should to get code to work for most compatibility issues.

Do you think I should remove the earlier ones and just use 20?

Another thing I’d like to ask about is why When I create a new project in Intellij it doesn’t give me the option to create a package for my classes. I checked earlier and the Intellij that I am using is up to date 2023.1.2

Yea it might be best to have only one version of JDK and JRE installed, just having the latest version should be fine. That might be whats causing the issues.

Yea I didn’t have that option either to create the package for the classes, it could be because of changes to intellij since the time that mosh did the recording. When I start a new project the whole sidebar is inside a root folder that is named from the project but mosh has his files inside the ‘com.codewithmosh’ package inside his root folder which is ‘Code’. Its pretty small issue though all you have to do it right click the src folder and add new package and call it ‘com.whatever’ and put your files there

1 Like

Thanks most helpful!

Even though I have only the latest installments of java and Intellij I still get these options in the language level settings in project structure. In the attached pic it shows all these different language levels that look like they correspond to the versions of JDK. Is that normal?

I recommend using “openjdk-16 Oracle OpenJDK version 21” and set the “Language Level” to the SDK DEFAULT. The system will automatically adjust the language level to match your JDK. See SCREEN CAPTURE # 1.

That said, you can always download JDKs. What they do is enable new language features or tweak syntax. So use the one you are more comfortable with (probably 17). See SCREEN CAPTURE #2.

Also, consider this: