Scope

Definition:

Scope refers to the lifetime and accessibility of a variable. How large the scope is depends on where a variable is declared. For example, if a variable is declared at the top of a class then it will accessible to all of the class methods. If it’s declared in a method then it can only be used in that method.

For more information, have a look at the Understanding Variable Scope and Using Modifiers With Variables.

Examples:

For example, the scope of the variable NUMBER_OF_HOURS_IN_A_DAY is the whole class. Whereas the scope of NUMBER_OF_DAYS_IN_A_WEEK is just the calculateHoursInWeeks method:


public class AllAboutHours{
   private final int NUMBER_OF_HOURS_IN_A_DAY = 24;

   public int calculateHoursInDays(int days)
   {
     return days * NUMBER_OF_HOURS_IN_A_DAY;
   }

   public int calculateHoursInWeeks(int weeks)
   {
      final int NUMBER_OF_DAYS_IN_A_WEEK = 7;
      return weeks * NUMBER_OF_DAYS_IN_A_WEEK * NUMBER_OF_HOURS_IN_A_DAY;
   }
}