0

I am having trouble with my application. Whenever I try to use the character 'var' it is not recognized by the IDE. I tried this thread here but it did not change my problem.

I made a method for retrieving users from a database by using a UserRepository and that is where the problem takes place:

    @Override
    public List<User> findAll() {

        var users = (List<User>) userRepository.findAll();

//        List<User> users = new ArrayList<User>(userRepository.findAll());

        return users;
    }

Settings

Any tips?

Lukas
  • 105
  • 1
  • 2
  • 9
  • 2
    You need to use Java 11 in the project structure (Language Level). Check that. – dan1st Apr 24 '21 at 21:05
  • 1
    the "Local Variable Type Inference" (i.e. var) has been introduced since Java 10, thus you need to use a JDK 10+ version. – Marco Tizzano Apr 24 '21 at 21:08
  • 1
    Please read: ["Why not upload images of code/errors when asking a question?"](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) --- Please [edit] the question and add the compilation error, as well as the definition of `userRepository::findAll`. – Turing85 Apr 24 '21 at 21:09
  • Thank you for the replies. I went into File > Project Structure > Modules and changed the language level of my project to 11. Now 'var' is recognised. Thank you for your help! – Lukas Apr 24 '21 at 21:22
  • Let's put everything in the answer, so other people can be helped by this. Please accept the answer. – Marco Tizzano Apr 24 '21 at 21:49

2 Answers2

3

The "Local Variable Type Inference" (i.e. var) has been introduced since Java 10, thus you need to use a JDK 10+ version.
If you don't have such a version, please download it by Oracle website and install it, then launch IntelliJ and go to "File->Project Settings->Project" and select the JDK version in "Project JDK" field.

Marco Tizzano
  • 1,726
  • 1
  • 11
  • 18
0

JDK version wrong. You should JDK 10+ in this idea project setting.

Zhefang Wu
  • 19
  • 6