Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Variable Scope

We will now get to familiarize ourselves on the variety of ways to declare a variable. It is very important to know that depending where you declared your variable, its scope and its life cycle will depend on it too.

  package com.example.core;

  /**
   * This demonstrates the scope of class variables, object 
   * properties and local variables.
   * 
   * @author Rolan Liwanag
   *
   */
  public class VariableScope {

	private String anAttribute;
	private static String classLevel;
	
	public static void main(String[] args) {
	  /*
	   * Let us focus on anAttribute and classLevel 
       * variables. One is declared as an object property 
	   * and the other is declared as class variable.
	   */
	  VariableScope v1 = new VariableScope();
	  v1.anAttribute = "QWERTY";
	  v1.classLevel = "QWERTY";
	  VariableScope v2 = new VariableScope();
	  v2.anAttribute = "YTREWQ";
	  v2.classLevel = "YTREWQ";
	  System.out.println("v1.anAttribute = " 
	    + v1.anAttribute);
	  System.out.println("v1.classLevel = " 
	    + v1.classLevel);
	  System.out.println("v2.anAttribute = " 
	    + v2.anAttribute);
	  System.out.println("v2.classLevel = " 
	    + v2.classLevel);
	  /*
	   * The object property anAttribute has 'QWERTY' and 
	   * 'YTREWQ' as the value for v1 and v2 respectively.
	   * The reason is, each object instance have 
	   * different copy of that variable. 
	   * 
	   * The class variable (variable declared as static) 
	   * is a class level variable. In this case, it is 
	   * the classLevel variable. For all the instances of
	   * the class VariableScope, it only has one copy of 
	   * that variable. This means, when we set the value 
	   * for object v1, classLevel was equal to 'QWERTY'.
	   * Then, when we set the value for object v2, 
	   * classLevel was changed to 'YTREWQ'.
	   */
	  System.out.println("\n------------------------");
	  v1.updateVariables();
	  System.out.println("v1.anAttribute = " 
	    + v1.anAttribute);
	  System.out.println("v1.classLevel = " 
	    + v1.classLevel);
	  System.out.println("v2.anAttribute = " 
	    + v2.anAttribute);
	  System.out.println("v2.classLevel = " 
	    + v2.classLevel);
	  /*
	   * Examining the output, only v2.anAttribute did not 
	   * change in value. The reason is, 
	   * v1.updateVariables() method only updated the value
	   * for v1.anAttribute and the class variable 
	   * classLevel.
	   */
	  System.out.println("\n------------------------");
	  v2.updateVariables();
	  System.out.println("v1.anAttribute = " 
	    + v1.anAttribute);
	  System.out.println("v1.classLevel = " 
	    + v1.classLevel);
	  System.out.println("v2.anAttribute = " 
	    + v2.anAttribute);
	  System.out.println("v2.classLevel = " 
	    + v2.classLevel);
	  /*
	   * This time, only the v1.anAttribute did not change 
	   * in value.
	   */
	  int i = 0;
	  if(i==0) {
        i++;
        System.out.println("The variable i is accessible " 
          + "within the if block of code." + i);
        int j = 0;
	  }
	  //The variable j is not accessible outside the if 
	  //block since it is outside the if block
	  //j++;
	  v1.methodParameter("abc123");
	}
	
	private void updateVariables() {
      this.anAttribute = this.anAttribute + "123";
	  VariableScope.classLevel = VariableScope.classLevel 
	    + "123";
	  /*
	   * v1 and v2 that are declared in main method are not 
	   * accessible here in this method. v1 and v2 are 
	   * local variables of main method.
	   */
	  //v1.classLevel = "not going to happen";
	}
	
	private void methodParameter(String param) {
	  System.out.println("method parameter : " + param);
	  //param is only accessible in this method.
	}
	
  }
                            


Knowing the scope of your variables will make your trouble shooting easier. It will make your program be more efficient. It is highly recommended in most cases to use local variables as much as possible because these are thread safe. These variables are also easier to be candidates for garbage collection which free up memory. A good tip in deciding the variable scope is to make it as narrow as possible. Let us say, you will only need a certain variable inside a for loop. Then, declare the variable inside the for loop. Do not declare it outside the loop or worst, make it as an instance variable.


Back    Next