Scope


Collapse Content

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:

BlueJ Scope Example

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.

For example, let's say you create a for loop and declare an index variable i within it (such as in the above code). That variable i will only be available within the for loop. Once the for loop finishes, i no longer exists.

These are the different types of variables and their scopes:

  • Instance Variables are declared inside a class but outside of any method. They are available to all of the code in the Class. They are used to store properties about an object (its "state"), such as the color of a Car.

  • Local Variables are declared and assigned a value within a method, and they only exist while that method is running. If a variable is declared within a smaller block of code (such as a for loop) it will only exist within that block of code.

  • Parameters are like local variables that are passed into a method. They are only available within that method.

Challenge

What will the following method print?

public void printNum(){
  int i = 0;       
  while(i<5){
     i = i + 1;
   }
   System.out.println(i);
}

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

What will the following Object print after being created?

colored-code

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • I have the correct answer now, but first of all I thought it would be 16. I didn't think the call to the doubleTotal method would actually change the value of the total variable, because the method is void and the new total doesn't get returned as an integer.

  • How it works then with void? It only do something there, change the value, but type for total is taken from the beginning of the class, when int total; is defined <

  • total is available to the whole instance, so doubleTotal will double it for all the code that uses it. In general, variables are available to the entire scope (marked with {}) that they are declared in.

    cont...
  • Thanks a lot as always

  • Can't do the last challenge because the picture isn't loading

    Image placeholder with title "colored-code"

  • Sorry about that, it's been fixed.

Contact Us
Sign in or email us at [email protected]