Default values
What is in a box that nothing has been put into yet? In the first lesson of this theme, the compiler did not let you read such a variable inside main. But variables declared right in the class get a different answer.
Picture two boxes. You set up the first one yourself and have not put anything in it yet, so the compiler does not let you look inside. The second one the warehouse prepared in advance, and it already holds a standard item: a zero, false or a "nothing" tag. Let's see the second box at work:
public class Main {
static int count;
static double price;
static boolean ready;
static String name;
public static void main(String[] args) {
System.out.println(count); // 0
System.out.println(price); // 0.0
System.out.println(ready); // false
System.out.println(name); // null
}
}The program compiles and prints four values, although none of the four variables was given a value.
Fields and default values
A variable declared right in the class, outside main, is called a field. The word static before it works as on main: the field belongs to the class itself. A constant such as static final int MAX_SPEED = 120; is a field too, only its value is given right away and does not change.
A field with no value gets its type's default value:
| Field type | Default value |
|---|---|
byte, short, int, long | 0 |
float, double | 0.0 |
char | the character with code 0, '\u0000' |
boolean | false |
String | null |
Let's walk through the program's output:
countprints as0, like any whole-number field with no value.priceprints as0.0, with a decimal point, because it is adouble.readyprints asfalse.nameprints asnull.
null is a special value meaning "nothing". It is not an empty string: there is no string in the field name at all, and println reports that with the word null.
This is where the box picture falls short. An empty box in real life holds nothing. But an int field is not empty: it holds a real number 0, and you can work with it like with any other.
Local variables
A variable declared inside main or another method is called local. It gets no default value. Let's compare a field and a local variable in one program:
public class Main {
static int total;
public static void main(String[] args) {
int local;
System.out.println(total); // 0
local = 7;
System.out.println(local); // 7
}
}Let's go line by line:
static int total;declares a field, and it holds0right away.int local;declares a local variable with no value.System.out.println(total)prints0: a field can be read right away.local = 7;puts a value into the local variable.System.out.println(local)prints7. Without the linelocal = 7;the program would not compile.
Why does a field get a value by itself while a local variable does not? This is a common interview question. The answer is:
- a local variable is visible only inside its method, so the compiler can check every read and catch a forgotten value before the program runs;
- a field can be read by different methods of the class in any order, so the JVM itself puts the default value into each field when it loads the class.
Common mistake
A field was declared without static:
public class Main {
int count;
public static void main(String[] args) {
System.out.println(count); // ✗
}
}error: non-static variable count cannot be referenced from a static contextThe program does not compile. A field without static belongs not to the class but to an object: a separate thing the program builds from the class's description while it runs. Nobody creates an object in this program, and main works without one, so it cannot read such a field. For now, write static on fields, as on main itself: static int count;.
Key points
- A field is a variable declared in the class outside
main; with no value, it gets the default value:0,0.0,falseornull. - A local variable gets no default value, and the compiler will not let you read it before the first assignment.
nullin aStringfield means there is no string there.- A field that
mainreads is declared with the wordstatic.