class Test {
public static void main(String[] args) {
private int x = 10;
public int y = 20;
protected int z = 30;
static int w
In short - none of the other modifiers make sense in that context. Saying a variable is public
, private
, protected
, or static
simply doesn't make sense in the context of a local variable that will go out of scope (and be garbage collected) once the method exits. Those modifiers are intended for class fields (and methods), to define their visibility (or in the case of static
, their scope).
final
is the only one that makes sense in the context of a local variable because all it means is that the variable cannot be modified after its initial declaration, it has nothing to do with access control.