class Knowing {
static final long tooth = 343L;
static long doIt(long tooth) {
System.out.print(++tooth + " ");
return ++tooth;
}
public static void main(String[] args) {
System.out.print(tooth + " ");
final long tooth = 340L;
new Knowing().doIt(tooth);
System.out.println(tooth);
}
}
Output is 343 341 340
Always local variable is of high priority than that of instance or class level variables.
So 340 gets passed to the method and gets incremented by 1 in doIt() method and prints it.
In
java, variables are passed by value. So the value of tooth in main will not get reflected (Also note that it is a final variable).So finally 340 gets printed.