What are the different scopes of a variable in Java, from a local variable in a block up to a static field?
Java has four practical levels of scope, and they nest.
Block scope covers local variables, method parameters and the resources of a try-with-resources block. A local variable is visible from its declaration to the end of the enclosing block, and cannot be used before that declaration point. Because a for loop body is its own block, the loop variable of an enhanced for is not visible after the loop, which is a genuinely useful property.
Method scope is the frame of a method: parameters and locals live here and die when the method returns. Nothing declared inside a method is visible to another method.
Class scope covers the fields of a class, which live as long as the instance does. Instance fields belong to an object; static fields belong to the class and are shared by every instance and by the class itself.
Two rules complete the picture. A local variable may shadow a field: the local wins inside its block, and the field is unreachable there without qualification. And a field declared in a superclass is visible in a subclass, though it may be shadowed by a field of the same name declared in the subclass.
The other thing to remember is lifetime versus scope. Scope decides where you can name a variable; lifetime decides how long the value survives. A static field has class lifetime but is not in scope for code that does not reference the class by name, and a local variable in a dead block is still in scope for the compiler even though its value is gone.