Variable initialization and its scope



This section describes variables are declared, scoped, and initialized in the java programming language.

Variables and Scope

You have seen two ways to describe variables: variables of primitive type or variables of reference type. You have also seen two places to declare variables: inside a method or outside a method but within a class definition. Variables defined inside a method are called local variables, but are sometimes referred to as automatic, temporary, or stack variables. You must initialize local variables explicitly before the first use. Method parameters and constructor parameters are also local variables but they are initialized by the calling code.

Variables defined outside a method are created when the object is constructed using the new Xxx() call. There are two possible types of these variables. The first kind is a class variable that is declared using the static keyword. Variables labeled static are created when the class is loaded and continue to exist for as long as the class is loaded. The second type is an instance variable that is declared without the static keyword. Instance variables continue to exist for as long as the object exists. Instance variables are sometimes referred to as member variables, because they are members of objects created from the class. The static variable is described later in this course in more detail. Both member variables and class variables are initialized automatically when they are created. Method parameter variables define arguments passed in a method call.

Each time the method is called, a new variable is created and it lasts only until the method is exited. Local variables are created when execution enters the method and are destroyed when the method is exited. This is why local variables are sometimes referred to as temporary or automatic. Variables that are defined within a member function are local to that member function, so you can use the same variable name in several member functions to refer to different variables. This is illustrated in the example on Code on

Code Variable Scope Example

1 public class ScopeExample {

2 private int i=1;


4 public void firstMethod() {

5 int i=4, j=5;

6 this.i = i + j;

7 secondMethod(7);

8 }

9 public void secondMethod(int i) {

10 int j=8;

11 this.i = i + j;

12 }

13 }

1 public class TestScoping {

2 public static void main(String[] args) {

3 ScopeExample scope = new ScopeExample();

4 scope.firstMethod();

5 }

6 }

Note – This code serves to exemplify scoping rules only. Reusing names in this manner is not a good practice.


Variable Initialization

You must initialize a variable in a Java program before you can use it. For stack variables it is easy for the compiler to check if the code assigns a value to such a variable before it attempts to use that value: The this reference and the method parameters have assigned values when the method executions starts. Local variables that are defined within the method code can be checked by looking at the method code only. So the compiler enforces that the method code cannot read a variable value before we have assigned one.

For object attributes on the heap, such a check is not easily possible: How can the compiler know whether a client of our class calls a setter method on an object before maybe some other client wants to call a getter method on it? So the only reasonable way to handle this problem is to enforce assigning an initial value already in the constructor. If the programmer does not provide a default value, the compiler will assign the default value. Table lists the default values for primitive and reference instance variables.


Note – A reference that has the null value does not refer to an object. An attempt to use the object it refers to causes an exception. Exceptions are errors that occur at runtime and are described in a later module.

Initialization Before Use Principle

While variables defined outside of a method are initialized automatically, local variables must be initialized manually before use. The compiler flags an error if it can determine a condition where a variable can be used before being initialized.

public void doComputation() {

int x = (int)(Math.random() * 100);

int y;

int z;

if (x > 50) {

z = y + x; // Possible use before initialization


y = 9;