In java, if a method has a final variable (not static), and If I am calling the method multiple times, can the final variable have different values in each call?
public void method1(String msg){
final ArrayList<MessageObject> list = method2(msg);
// code that uses list (example just prints)
}
method1("one")
method1("two") are two calls,
if method2() returns different lists for each input, is the above code valid (with respect to final modifier?)
Yes, absolutely.
finalonly means that that particular variable can’t be assigned a different value.If you have multiple calls to the method (whether via recursion or multiple threads) those are entirely separate variables.