Programming in Java on Your Computer
- Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Premium Content - Free Preview
As mentioned in Accessors and Modifiers, some variables are only available within a certain method, while other variables are available to all of the code in the Class.
The area of code that a variable is available to, is known as the scope. A scope (or "block") is normally specified by curly-braces in Java. BlueJ marks the different scopes with different colors, so you can see them more clearly:
You can view the different levels of scope as different colors, from the Class to the Methods to the Loops within methods. (The code editor on this site does not mark scopes with colors, but it does put little arrows on the side for collapsing and expanding them.)
Any variable declared in a specific scope is available only to that scope (except for non-private instance variables). Code outside of that scope cannot access that variable. In fact, the variable gets deleted once its scope of code finishes running.