The var keyword in Java
Java var keyword and inferred types. @Scrumtuous Darcy DeClute

The var keyword in Java

Java's var keyword allows developers to declare local variables without specifying a data type such as int, long, String or char.

A new language feature introduced in the 2018 JDK 10 release, the Java var keyword performs type inference.

Java, var and inferred types

Type inference means Java will guess the data type when the variable is created, and enforce that typing throughout the program.

With Java's var keyword combined with type inference, Java programs remain strongly typed while the developer is relieved of the burden of explicitly stating the type of each variable.

Variable Declarations in Java

Here's how the Java var keyword looks when used in a program

var theMagicNumber = 7;
var theGuess = 5;         

Yes, this is Java.

  • It's not JavaScript
  • It's not C octothorpe
  • It's not Turbo Pascal
  • It's not Swift
  • It's not Kotlin

This is Java, and this is how you can declare local variables in Java.

Java's new language features

I'm always surprised by how many Java developers are unaware of the fact that that you can declare local variables in Java with the var keyword.

(Actually, var is not a keyword. It's a reserved word. But let's not get into that.)

The var keyword makes Java code look cleaner, but it also has another great benefit. It makes Java easier to learn!

In my article on how to write your first Java program, I used the var keyword extensively. It just makes learning easier and more fun. If you want to learn to program, Java is definitely the way to go! (Video tutorial here)

A good rule of thumb when it comes to the var keyword is that it can be used inside methods to declare local variables.

Where is Java's var not allowed?

However, the var keyword is not allowed in all context. Here are six instances where the Java var keyword is not allowed:

  1. Method Parameters
  2. Field or instance variables
  3. Return types for methods
  4. Parameters for constructors
  5. Lambda parameters
  6. Array declarations

So there are definitely some compiler enforced rules about when to use Java's var keyword.

Java var keyword best practices

There are also some best practcies surrounding the use of Java's var keyword, such as:

  • Use Java's var to improve readability
  • Don't overuse Java's var and make things unreadable
  • Use clear and unambiguous variables names
  • Combine final and var when appropriate
  • Avoid using var in nested blocks of code. It can get confusing
  • Use explicit types if the data type is not obvious, especially with polymorphic components
  • Be aware of any team standards that might exist pertaining to Java's var keyword
  • Be consistent with the use of var
  • Make sure your IDE supports its use (Most IDEs today do)

So there you go! Java just keeps getting better and better, and Java's var keyword is a great example of that!

Get Scrum Master Certified!

By the way, my Scrum Master Certification Guide is now available on Amazon.

If you want to learn more about Scrum, and perhaps even get you PSM1, CSM or Product Owner certification, it's the right book for you!

It's helping lots of people get Scrum certified!












Greg Munt

Software marmite with a dash of autism

1 年

It's sad to see that Java is just sitting back and waiting for something new to emerge, then copying it. This sort of thing has been in Groovy (another JVM language) for years.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了