Java Variables, Types of Variables, Local Variable Type Inference
Java Variables
Java variables can be used in two way :
Java variables without type inference
Java variable without type-inference can be declared as explicitly, it needs to specify the type of variable explicit.
Java variables with type inference
Beginning with JDK 10, new features added by the JDK 10 was support for local variable type inference supported. With type-inference, it’s now possible to declare a variable as an inferred type of its initializer instead of declaring as explicitly. To use a type-inference local variable, the context-sensitive identifier var is used. It must know that java type-inference cannot be a static variable. It required to initialize a local variable type-inference.
Table of Contents
Java Types of Variables
Java Variables Without Type Inference
Java Local Variable
In Java 10, the local variable is a variable that given a local scope. Scope of the local variable is within any function or block, that overrides the same name variable declared as a larger scope block.
public class LocalVariableDemo {
public static void main(String[] args){
int score; // local variable declared
score= 90; // local variable initialized
System.out.print(score); // local variable used
}
}
Output of the Above Program :
90
Java Static Variable
In Java 10, the global variable can be achieved by the static keywords. An instance variable can be declared as static. When the object of its class declared, it doesn’t create a copy of a static variable. Instead, all instances of that class share the same static variable block.
public class StaticVariableDemo {
public static void main(String[] args){
static int score; // use static keywords to declare a global variable
score= 90; // Global variable initialized
System.out.println(score); // Global variable used
score = 80; // Global variable re-initialized
System.out.println(score); // Global variable re-used
}
}
The above example shows the use of static variables, static variables created as “score” which share the same memory block.
Output of the Above Program :
90
80
Java Instance Variable
In Java 10, the java instance variable is a data member of the class, the class encapsulates the data members and functions. The instance variable is accessible to all functions inside the same class. An example of the Instance variable is given below:
public class InstanceVariableDemo {
private int score; // instance variable should be outside the function
public static void main(String[] args){
score= 90; // Global variable initialized
System.out.println(score); // Global variable used
score = 80; // Global variable re-initialized
System.out.println(score); // Global variable re-used
}
}
The above example gives the illustration of the java instance variable. An instance variable is created called a score which has a private access modifier.
Output of the Above Program :
90
80
Java Variables With Type Inference
In java 10 or above there is only a local variable supported for type inference, which cannot be able to use static keywords with type inference. you just need to initialize a local variable type-inference, type-inference local variable can’t be declared empty and initialize it later. The following example shows, how type inference can be used in local variables:
Java Local Variable type-inference
public class LocalVariableInferenceDemo {
public static void main(String[] args){
var score; // local variable declared
score= 90; // local variable initialized
System.out.print(score); // local variable used
}
}
Output of the Above Program :
90
Local Variable Type Inference Limitations
There are certain types of limitation in Local Variable Type Inference, some of them shown below:
Can’t use ‘var’ type-inference variable without initialize
If the var type local variables is not initialized, it will raise an error because the compiler is unable to infer the type of variables.
In case, if user-declared but not initialize the var as shown below :
var score; // Wrong, Initializer required.
Following error will show :
C:\codenaive>javac variable.java
variable.java:6: error: cannot infer type for local variable score
var score;
^
(cannot be used "var" on variable without any initializer)
1 error
‘var’ type local variable cannot use null as an initializer
If the var type local variable is initialized to null, it will raise an error because the compiler is unable to infer the type of variables.
var score = null; // Wrong, Initializer required.
Following error will show :
C:\codenaive>javac variable.java
variable.java:6: error: cannot infer type for local variable score
var score=null;
^
(variable initializer is 'null')
1 error
Only one variable can be declared at a time
Cannot declare multiple variables with inference type, it will raise an error because the compiler is unable to infer the type of variables.
var scoreValid=0; // Valid
var resultValid=0; // Valid
var score=0,result=0; // Wrong, Inference type variable should have only one variable.
Following error will show :
C:\codenaive>javac variable.java
variable.java:6: error: 'var' is not allowed in a compound declaration
var score=0,result=0;
^
1 error
With an array user can only declare it, cannot initialize it.
var myArrayRight = new int[10]; //Valid , Declartion of Array using type inference
var myArrayWrong = {'c','o','d','e','n','a','i','v','e'}; // Wrong, Declartion.
Following error will show :
C:\codenaive>javac variable.java
variable.java:6: error: cannot infer type for local variable myArrayWrong
var myArrayWrong = {'c','o','d','e','n','a','i','v','e'}; // Wrong, Declartion.
^
(array initializer needs an explicit target-type)
1 error
Local variable type inference cannot be used in lambda expression as initializers.
Local variable type inference cannot be used in method references as initializers.
Leave a Reply