Scope


Local Variables

A “local variable” is declared inside a method, and is not accessible to code outside that method. Different methods can have local variables with the same name, because the methods cannot see each other’s local variables.

For example...

/**

 * The mrBrightside method has a local variable named lyrics.

 */

public static void mrBrightside() {

   // Scope of the mrBrightside lyric variable starts here

   lyrics = “Open up my eager eyes\n'Cause I'm Mr. Brightside”;

}// Scope of the mrBrightside lyric variable ends here

/**

 * The human method ALSO has a local variable named lyrics.

 */

public static void human() {

   // Scope of the human lyric variable starts here

   lyrics = “Are we human?\nOr are we dancer?”;

}// Scope of the human lyric variable ends here

A “block” of code is a section of code enclosed by brace (squiggly) brackets.

From this example, you can conclude that a variable’s “scope” goes from where it is declared until the block of code in which it is declared ends. This means if we were to try to print out the variable lyrics from the main() method, we wouldn’t be able to.

For example, it is true of loops too...

{ int i = 1;      // Scope of i starts here

  …               // Can’t use j or k here; they are not declared yet

  for(int j = 0; ...) {  // Scope of j starts here

    int k = 1;    // Scope of k starts here

    …             // Can use i, j, or k here!

  }               // Scopes of j and k end here

  …               // Still in scope of i here, but not j or k anymore

}                 // Scope of i ends here

Lifetime of a local variable

A method’s local variables exist only while the method is executing. This is known as the “lifetime” of a local variable.

When the method begins, its local variables and its parameter variables are created in memory, and when the method ends, the local variables and parameter variables are destroyed.

This means that any value stored in a local variable is lost between calls to the method in which the variable is declared. You cannot access these local variables outside the method they’re declared in!

Global variables (fields)

To be clear, there aren’t actually global variables in Java.

A “global variable” by definition is a variable that is accessible to all parts of the program, usually declared at the start of the code.

Technically, since Java is a true Object-Oriented language, there’s no such thing as a global variable because everything is inside blocks of code - the biggest we have seen are classes. As we just learned, the lifetime of a variable ends when the block ends.

But right now, we are only working in one class, Main, and so we can declare special variables called “fields” that will be accessible to all methods in the Main class. This will seem “global” to us, since we only have one large block, Main.

Fields

To be even clearer, I don’t love fields or global variables in the Main class.

It is best practice to declare a variable closest to where you’re actually going to use it. This is more like an FYI for now. When we go to make our own classes later, fields will be nice.

To declare a field, we can write the variable declaration outside all methods, but still inside the Main class.

The only catch is that you have to make the variable static. (A dumb thing that I don’t want to explain because really you shouldn’t really do this).

For example…

public class Main {

  static int accessibleInt;   // Scope of accessibleInt starts here

  public static void main(String args[]) {

    // This will work because accessbleInt is a field 
   System.out.println("From the main method: " + accessibleInt);

    example();

  }

  public static void example() {

    // accessibleInt can STILL be used here, because it's a field 

    System.out.println("From the example method: " + accessibleInt);

  }

} // Scope of accessibleInt ends here, where the Main class ends