1.4 Simplifying Java with var


22/05/25

8 mins

Simplifying Java with var: Local Variable Type Inference

With the release of Java 10, working with variables in Java got much simpler and less repetitive. The key to this new simplicity is the var type identifier. If you've ever felt typing out variable types was tedious or made your code harder to read, var solves exactly that problem.

What is var?

The var identifier allows Java programmers to let the compiler figure out the type of a local variable when it's declared. Once the type is determined by the compiler, it cannot change, and the variable becomes just as "strongly typed" as it would be with a regular type declaration.

Before Java 10, you had to write out the type every time you declared a variable, like so:

String message = "Hello world!"; Path path = Path.of("debug.log"); InputStream stream = Files.newInputStream(path);

This can get redundant quickly, especially when the type is already obvious from the code.

With var, the compiler does the work for you:

var message = "Hello world!"; var path = Path.of("debug.log"); var stream = Files.newInputStream(path);

All three variables are correctly typed—var doesn't make Java "loosely typed" or remove compile-time type checking.

Practical Examples with var

Using var isn't just about saving keystrokes; it also makes your code cleaner and easier to read, especially with complex types. For example:

var list = List.of("one", "two", "three", "four"); for (var element : list) { System.out.println(element); }

Here, list is automatically inferred as a List, and element as a String. This is especially helpful when looping or using Java collections, where type declarations can get long and complicated.

Another example with files:

var path = Path.of("debug.log"); try (var stream = Files.newInputStream(path)) { // process the file }

Notice how var works smoothly in a try-with-resources block.

Where Can You Use var?

  • Only for local variables (those inside methods, constructors, or initializer blocks)
  • Works in basic assignments, for-loops, and try-with-resources.

Restrictions and What You Can’t Do with var

  • Not for fields: You can’t use var for class or instance fields.
  • Not for method parameters: You can’t use var in method or constructor parameter lists.
  • Requires initializer: Since the compiler needs to know the type, you must initialize the variable on the same line. For example, var message = "hello"; is OK, but just var message; will fail.
  • Cannot initialize to null: Because null has no type, var something = null; produces a compile error.
  • Not suitable for unclear types: If it’s not obvious what the type will be from the initializer, using var can make code harder to read.

Example of incorrect usage that will give compiler errors:

public class User { private var name = "Sue"; // Error: cannot use var for fields public void setName(var name) { // Error: cannot use var as parameter type this.name = name; } } public String greetings(int message) { var greetings; // Error: must initialize with a value // ... }

Why Use var?

  • Reduces code clutter—no need to repeat the type when it's obvious
  • Improves readability for code with long or complex type definitions
  • Keeps strong typing—the compiler still checks type safety and prevents errors

Conclusion

var brings modern convenience to Java by letting you write local variables without always spelling out their types. It makes your code shorter and often clearer, but it doesn't change Java's strict type safety. Use it thoughtfully—where it helps, not where it hides what your code is doing.

Happy coding!


PREVIOUS: 1.3 Java Arrays Explained Simply

NEXT: 1.5 Mastering Operators in Java