By Doug Lowe

In Java, local variables are not given initial default values. The compiler checks to make sure that you have assigned a value before you use a local variable. The following example program won’t compile:

public class testApp


public static void main(String[] args)


int i;

System.out.println("The value of i is " + i);



If you try to compile this program, you get the following error message:

C:\Java\ error: variable i might not have been initialized

System.out.println("The value of i is " + i);


To avoid this error message, you must initialize local variables before you use them. You can do that by using an assignment statement or an initializer.

Unlike local variables, class variables and instance variables are given default values. Numeric types are automatically initialized to zero, and String variables are initialized to empty strings. As a result, you don’t have to initialize a class variable or an instance variable, although you can if you want them to have an initial value other than the default.

Initializing variables with assignment statements in Java

One way to initialize a variable is to code an assignment statement following the variable declaration. Assignment statements have this general form:

variable = expression;

Here, the expression can be any Java expression that yields a value of the same type as the variable. Here’s a version of the main method from the previous example that correctly initializes the i variable before using it:

public static void main(String[] args)


int i;

i = 0;

System.out.println("i is " + i);


In this example, the variable is initialized to a value of zero before the println method is called to print the variable’s value.

For now, you can just use simple literal values, such as 0 in this example.

Initializing variables with initializers in Java

Java also allows you to initialize a variable on the same statement that declares the variable. To do that, you use an initializer, which has the following general form:

type name = expression;

In effect, the initializer lets you combine a declaration and an assignment statement into one concise statement. Here are some examples:

int x = 0;

String lastName = "Lowe";

double radius = 15.4;

In each case, the variable is both declared and initialized in a single statement.

When you declare more than one variable in a single statement, each variable can have its own initializer. The following code declares variables named x and y, and initializes x to 5 and y to 10:

int x = 5, y = 10;

When you declare two class or instance variables in a single statement but use only one initializer, you can mistakenly think that the initializer applies to both variables. Consider this statement:

static int x, y = 5;

Here you might think that both x and y would initialize to 5. But the initializer applies only to y, so x is initialized to its default value, 0. (If you make this mistake with a local variable, the compiler displays an error message for the first statement that uses the x variable because it isn’t properly initialized.)