范围分区
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
met
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;
}
}
范围分区