class Demo3 {
// instance variable
var x = 50;
public static void main(String[] args)
{
System.out.println(x);
}
}
Reference: https://www.geeksforgeeks.org/var-keyword-in-java/
class Demo3 {
// instance variable
var x = 50;
public static void main(String[] args)
{
System.out.println(x);
}
}
Reference: https://www.geeksforgeeks.org/var-keyword-in-java/
On
var can only be used for local variables. fields etc. were explictly excluded, see JEP 286.
In an Update on JEP 286, Java Language Architect Brian Goetz expressed a strong opinion about using it for fields:
"[...] is fields -- but applying type inference there would be foolish."
You don't. The
varkeyword can only be used within a method scope while also being initialized immediately. Hence, instance variables can not be type inferred.