tellarwind
tellarwind

Reputation: 23

How to fix this: cannot find symbol var in StaticTest

I am trying to compile a code from book about Java and I have a problem with var. I can't compile. What's wrong? I have verified and the problem still exists. Can you check this code below?

public class StaticTest
{ 
    public static void main(String[] args) 
    {    
        var staff = new Employee[3];

        staff[0] = new Employee("Tomasz", 40000);
        staff[1] = new Employee("Dariusz", 60000);
        staff[2] = new Employee("Grzegorz", 65000);


        for(Employee e : staff)
        {
            e.setId();
            System.out.println("name=" + e.getName() + " ,id=" + e.getId() + " .salary=" + e.getSalary());
        }

        int n = Employee.getNextId();
        System.out.println("Następny dostępny identyfikator=" + n);

    }
}

Upvotes: 1

Views: 1216

Answers (1)

Dominic
Dominic

Reputation: 90

The possibility to use var to declare a variable was added in Java 10. You have to update your JDK to version 10 or above. Otherwise you can use Employee[] staff = new Employee[3]; instead.

Upvotes: 1

Related Questions