java-tut-variables-data-type-var-type-name-1
public class SampleClass{
//invalid: can't use var
//keyword as type of
//instance variable
//var myVar = false;
void meth1(){
//var can be used in this
//block
var str = "String";
//var can be used in for loop
//parentheses
for(var i = 0; i < 3;i++)
System.out.println(str);
}
//invalid: var can't be used as
//type of method and constructor
//parameter
//void meth2(var param){}
public static void main(String[]args){
//var can be used here 'cause main()
//is a method like meth1()
var chararacter = 'c';
//var can't be used as type of an array
//var[] myStr = new String[]{"A","B"};
String[] str = new String[]{"A","B"};
//var can be used in for-each loop
//parentheses
for(var s : str)
System.out.println(s);
}
}
//invalid: can't use var
//keyword as type of
//instance variable
//var myVar = false;
void meth1(){
//var can be used in this
//block
var str = "String";
//var can be used in for loop
//parentheses
for(var i = 0; i < 3;i++)
System.out.println(str);
}
//invalid: var can't be used as
//type of method and constructor
//parameter
//void meth2(var param){}
public static void main(String[]args){
//var can be used here 'cause main()
//is a method like meth1()
var chararacter = 'c';
//var can't be used as type of an array
//var[] myStr = new String[]{"A","B"};
String[] str = new String[]{"A","B"};
//var can be used in for-each loop
//parentheses
for(var s : str)
System.out.println(s);
}
}