Final And Var

Use final when a variable should receive a value once. Use var when the initializer already makes a local variable's type obvious.

var asks the compiler to infer a type; it does not make Java dynamically typed. In the example, gameTitle is still a String for its entire scope.

Fixed and inferred types

public class Main {
    public static void main(String[] args) {
        final int maxPlayers = 4;
        var gameTitle = "Skyward";

        System.out.println(maxPlayers);
        System.out.println(gameTitle);
    }
}

Output:

4
Skyward

maxPlayers cannot receive another value after its declaration. The initializer "Skyward" makes the type of gameTitle clear, so writing String again would add little information.

var works only for local variables with an initializer. It cannot be used for a field, a method parameter, or a variable initialized with plain null.

Choose a sensible use for final, then one for var.