Java

What is the var Keyword in Java

In basic Java programming, one of the core concepts that each developer needs to understand is the usage and limitations of the “var” keyword. This particular keyword is a prerequisite in order to move on to advanced concepts. It allows the programmer to initialize the values comprising multiple data types conveniently.

This write-up will illustrate the usage of the “var” keyword in Java.

What is the “var” Keyword in Java?

Java 10” introduced a new approach to declaring variables with the help of the “var” keyword. The type inference in the var keyword is utilized to analyze the variable’s data type automatedly.

Example 1: Applying the Unassigned “var” Keyword in Java

In this example, the behavior of the “var” keyword can be checked without assigning a value:

var name;

System.out.println(name);

In the above code, simply associate the “var” keyword with the stated variable without initializing and displaying it.

Output

In the above output, it can be observed that an error is logged on the console since the value is uninitialized.

Example 2: Utilization of the “var” Keyword as an Instance Variable

The “instance variables” are declared inside the class but outside the method, constructor, and block. In this particular example, a check can be applied to the discussed keyword by initializing it outside the scope of main(as an instance) and invoking it in “main”:

public class Example {

var num = 50;

public static void main(String args[]) {

System.out.println(num);

}

}

In the above lines of code, initialize the stated integer value with the help of the “var” keyword as an instance variable and invoke the initialized value in the “main”.

Output

This outcome indicates that the “var” keyword cannot be utilized to initialize instance variables.

Example 3: Utilization of the “var” Keyword With Different Data Types

In this particular illustration, the discussed keyword can be used to initialize different data type values:

var integer = 50;

var floatnum = 5.25;

var bool = true;

var character = 'J';

var stringval = "Linuxhint";

System.out.println("The integer value is: "+integer);

System.out.println("The float value is: "+floatnum);

System.out.println("The boolean value is: "+bool);

System.out.println("The character value is: "+character);

System.out.println("The string value is: "+stringval);

In the above code block, simply initialize the integer, float, boolean, character, and string values, respectively and display them one by one.

Output

Limitations of the “var” Keyword

Following are some limitations to consider before using the “var” keyword:

  • It cannot be used to initialize the local variables within the methods.
  • It cannot be utilized to declare/specify method parameters.
  • Can’t be used to specify the method’s return type.

Conclusion

The “var” keyword can be used to declare variables in Java and the type inference in it can be utilized to determine the variable’s data type automatically. This keyword cannot be used as “unassigned”, or as an “instance variable”. However, it supports all the data type formats. This blog discussed the usage of the “var” keyword in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.