Why always variables needs to be final in public class and main() method
It doesn’t. It’s a method-local variable, so it can’t have any access modifiers, but it can be either final or non-final. It has nothing to do with the enclosing class being public or the enclosing method being main()
. It’s just because it’s a method-local variable. Only member variables can have access modifiers.