Sub topic 10
Default Values and Definite Assignment
A variable has a value after it is initialized. Initialization is the first assignment of a value to a variable.
In Java, some variables start with a default value. Others must be assigned before you can read them. Knowing the difference helps you avoid errors and choose meaningful starting values.
Fields get default values
A class is a blueprint for creating objects. An object is a created item that can hold data. A field is a variable declared directly inside a class, outside any method. A method is a named block of code.
Java gives fields default values before any explicit initialization runs.
| Field type | Default value |
|---|---|
| `byte`, `short`, `int`, `long` | `0` |
| `float`, `double` | `0.0` |
| `char` | The character U+0000 |
| `boolean` | `false` |
| Reference types, such as `String` | `null` |
A reference is a value that can point to an object. The value null means it does not point to an object. The default char value is a character, not a null reference.
New arrays, which hold sequences of values, also give their elements defaults based on the element type.
Local variables must be assigned
A local variable is declared inside a method or another block of code. Java does not give it a default value that you can read. You must assign it first.
This rule is called definite assignment: Java checks that a local variable has a value on every possible path to where it is read. A path is a possible sequence of steps through the code.
A parameter is a variable that receives a value when a method is called. It is already assigned when the method starts.
Worked example
Save this program as DefaultValues.java, then compile and run it.
class DefaultValues {
int count;
boolean ready;
String name;
public static void main(String[] args) {
DefaultValues sample = new DefaultValues();
System.out.println(sample.count);
System.out.println(sample.ready);
System.out.println(sample.name);
int result;
if (sample.ready) {
result = 10;
} else {
result = 20;
}
System.out.println(result);
// int missing;
// System.out.println(missing);
}
}The output is:
0
false
null
20The first three values are defaults for the fields of sample. The local variable result has no default. It is safe to print because both branches assign it a value. Since ready is false, the else branch runs.
The last two lines are comments, so Java ignores them. If you remove their // marks, the program will not compile: missing is read without being assigned.
Check every path
If you remove the else branch, result would be assigned only when sample.ready is true. Java would reject the later print statement because there is a path where result has no value.
You can fix that by assigning result before the if, or by assigning it in both branches.
Exercise
A method declares int score; and then tries to print score. What must change?
Show answer
Assign score a value before printing it. A local variable cannot be read until it is definitely assigned.