Use ‘var’ to declare variables in Java !!!

Hello all ! Can you guess what will be the output of the following code snippet in Java?

OK !!! You might have the following answers :

Compile time error (var is not a type)OR
o/p = 30

For me , it is successwith a valid output as 30. Suddenly you might be thinking “am i crazy !!!”.

Yes I am a crazy developer. In the above code snippet, there is no error if you are using Java-10. (Even if you have Java-8 , you can also achieve this ; Read completely, you will figure it out.)

If you have coded in Scala, Go, C#, Kotlin or simple in Javascript, then you know that they all have some kind of local variable type inference already build in.

e.g. Javascript has let and var, Scala and Kotlin have var and val, C++ has auto, C# has var, and Go has support via declaration with : = the operator.

var’ In JAVA

Why ‘var’ in Java ?

Java developers have long been complaining about the degree of boilerplate code and ceremonies involved while writing Java code.

Example :

In below example we know that the RHS is a String type , still we are writing String on LHS.

String str ="Java";

Same in below example, we are assigning a list of String values, still we need to specify the type .

List<String> list = Arrays.asList("Deb","Honey","Rosalin");

Here we are creation a Customer type on RHS, but still on LHS, we need to write Customer as type.

Customer customer = new Customer();

The use of var also makes your code concise by reducing duplication, e.g. the name of the Class that comes in both right and left-hand side of assignments as shown in the following example:

Customer customer = new Customer();// infers Customer
var bos = new ByteArrayOutputStream(); // infers ByteArrayOutputStream
var str ="Java";// infers Stringvar list = Arrays.asList("Deb","Honey","Rosalin"); // infers List<String>

How to achieve the same in Java-8

Cool !!! As per java -8 , officially it is not supported(supported from Java-10). But yes, we can still achieve it . If you want to achieve ORM tool as Hibernate, you are going beyond the java and using a third party provided tool. Same as we can achieve it using a third party dependency Lombok plugin.

Refer for lombok : https://projectlombok.org/features/var

Points To Remember

So now you know tat how can you declare local variables without declaring the type in Java 10. It is time to consider some important factors before you start writing for production code.

  • This feature is built under JEP 286 — Local-Variable Type Inference.
  • The var allows local variable type inference, which means the type for the local variable will be inferred by the compiler.
  • The local variable type inference (or Java 10 var type) can only be used to declare local variables, e.g. inside methods, on initializer code block, indexes in the enhanced for loop, lambda expressions, and local variables declared in a traditional for loop.
  • You cannot use it for declaring formal variables and return types of methods, declaring member variables or fields, constructor formal variables, or any other kind of variable declaration.

Risks and Assumptions

Risk: Because Java already does significant type inference on the RHS (lambda formals, generic method type arguments, diamond), there is a risk that attempting to use var on the LHS of such an expression will fail, and possibly with difficult-to-read error messages.

case -1var x;^('var' on a local variable requires an initializer expression)case-2var f = () -> { };^(lambda expression needs an explicit target-type)case -3var m = this::l; (error: cannot infer type for local variable m)

That’s all about var in Java 10, an interesting Java 10 feature that allows you to declare local variables without declaring their type. Like other language feature, local variable type inference can be used to write both clear and unclear code. The responsibility for writing clear code lies on the developer. See more for style guidelines of var.

You might also be interested in the related tutorials:

Technology Enthusiast —Java8, Spring Boot, Micro-services, DDD, Web, Mobile, and Cloud-Native. Passionate about designing and developing scalable software.