5.8 Scope and Access
Think back to some of the examples where you had variables in methods. Some of these variables had the same name as other variables in your program, but they didn’t have the same values. How can that be? Each variable has a specific scope and access level where that variable is defined. Outside that scope, a different variable can be defined with the same name. In this section, you will look at how variable scope is determined.
When declaring a variable inside the body a constructor or a method, you create a local variable for that method. What does this mean? By local variable, it means that the variable is only defined for that specific constructor or method. These variables may only be used within the constructor or method and cannot be declared to be public or private.
Likewise, another variable with the same name can be used outside the constructor or method and it will be considered a different variable.
Sometimes you will see a local variable inside a method or constructor sharing the same name as an instance variable. When this happens, the variable name in the method by default will refer to the local variable instead of the instance variable.
When variables are declared as formal parameters, they act the same as a variable that has been declared inside a constructor or method. That means that they are only available inside the constructor or method where they are declared and that they will take precedence over any instance variables that have the same name.
Method decomposition is the process of breaking down large problems into smaller sub-problems by creating methods to solve the individual sub-problems.
For example, if you want to create a program that asks users a random trivia question and informs them if they got the answer correct, you could break the problem into several parts:
- Build a Trivia class.
- Create Program that uses Trivia class.
You can then break each of these problems into several parts. For example, the program that uses the trivia class might break into the following parts:
- Create a Trivia Object
- Ask User a random trivia question
- Allow User to Respond
- Check if Response Matches Answer
- Provide User with a Reply
By breaking the problem down into smaller parts, you create sub-problems that are easy to solve and combined together, they can solve a much more complicated problem.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
What would be printed the first time printGreeting was called from main?
For this exercise, we are going to take a look at an alternate Calculator
class, but this one is broken. There are several scope issues in the calculator class that are preventing it from running.
Your task is to fix the Calculator
class so that it runs and prints out the correct results. The CalculatorTester
is completed and should function correctly once you fix the Calculator
class.
Vocabulary
Term | Definition |
---|---|
Method Decomposition | The process of breaking down large problems into smaller problems, each with a method that defines a subproblem in the larger problem. |
Shadowing | If two variables within the same scope have the same name, the variable with the more specific scope will be called. |
Local Variable | A variable that is defined in a method or constructor. It only exists in the context of the method that it belongs to. |