Scope refers to a variable's area of accessibility. It determines where a declared variable can be used. Attempting to use a variable outside its scope will give you a compile-time error.

As a rule of thumb, you should always use the principle of least privilege when determining the scope to give a variable. According to this principle, a variable should have only as much visibility as it needs to get its job done and no more.

Rules of Scope

There are four basic rules which govern the scope of declarations.

Rule 1

When you declare a parameter, its scope is within the body of the function.

        String withdraw (int amount){
// this is the scope of the parameter "amount" i.e from the left brace ,{, to the the right one, }
}

Rule 2

The scope of a variable declared in a function is from the part where it's declared to the end of the function body. This type of variable declared within a function's body is called a local variable. Therefore, this variable has local scope.

        String withdraw (int amount){
String currency;
int transaction_fee = 2; // this is a local variable and it's scope begins from here till the right brace, }
}

Related: What Is Object Oriented Programming? The Basics Explained in Layman's Terms

Rule 3

A variable declared in the header of a for statement also has local scope.

        for ( int y=11;y <20; y++){
// variable y has local scope
}

At this point, it's important to mention that you'll get a compile-time error if your method has two local variables with the same name.

Rule 4

The scope of a method or a class's field is the entire class body. This enables a class' instance methods to be able to use the class' methods or fields.

Look at the example below. It's a fully working code implementation of the rules above. You can compile and run it on your PC.

        class Account {
int y = 2; // class field

static void withdraw(int amount){
String z;

for(int y=1; y<=3; y++){ // line 7
System.out.println("\n Remember to hide your PIN.");}
}

public static void main(String args[]){
withdraw(50); }
}

Notice the class field and the code on line 7. If you declare a parameter or local variable using a name similar to that used by one of the class fields, execution will still continue normally and without errors. This is known as shadowing.

In the example above, the variable y is declared twice and yet the compiler doesn't flag off any errors. The compiler first "hides" the class fields until the block is executed.

Related: The 4 Access Modifiers Explained in Java

Sometimes you may need to access a shadowed field in the block. This can be done in two ways:

a) You can use the keyword this if you are dealing with an instance variable. See the example below:

this.x: x is an instance variable

b) If the variable you are trying to access is a static class variable, use the syntax below to access it:

        ClassName.x // x is a static variable
    

Class Scope

Normally, each object has its own instance variables. There are scenarios when you need the same copy of the variable shared with all instances. In this case, you'll need to make them static.

Class members defined with the keyword static have class scope. Therefore, static variables have class scope. They can be accessed by all of the class's methods. To access a class's public static variables use:

        ClassName.variable_name
    

On the other hand, a class's private static variables can only be accessed by the class's methods.

Get Classes and Constructors in Your Scope

Sometimes you may require a variable that, when accessed after initialization, isn't modified. In case you have the need to do so, use the keyword final. Just place it before the variable you're declaring so as to use it.

        final double PI = 3.14159;
    

Trying to modify a variable declared as final will give off a compile-time error. It's also important for you to know that you must immediately initialize a final variable. You cannot choose to initialize it later on.

At this point, you should be looking at learning constructors and classes in Java.