Saturday, December 28, 2019
Scope - Definition for the Java Term
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 variableNUMBER_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; }}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.