Fields & state in Java
Instance fields hold each object's state.
State lives in fields
Instance fields are variables declared inside a class but outside any method. Together, their values are the object's state. Each object keeps its own copy, and fields live as long as their object does.
class Player {
String name; // field
int score; // field
}Free default values
Fields always start with a default value: 0 for numbers, 0.0 for decimals, false for booleans, '\u0000' for chars and **null** for references. No garbage, ever.
Fresh out of the box
Nothing is assigned. What does this print?
class Robot {
int battery; boolean on; String name;
}
void main() {
Robot r = new Robot();
System.out.print(r.battery + " ");
System.out.print(r.on + " ");
System.out.println(r.name);
}0 false nullCompile error0 false
Show the answer
0 false null. Fields get defaults automatically, and string concatenation turns the null reference into the text "null".
Now a local variable
Locals are declared inside a method. What happens?
int count;
System.out.println(count);0nullCompile error
Show the answer
Compile error: "variable count might not have been initialized". Local variables get no default ā you must assign them before reading.
Methods change their own object
An instance method works on the object it's called on. a.inc() changes a's field and nobody else's.
class Counter {
int value;
void inc() { value++; }
}
// a.inc(); a.inc(); b.inc();
// a.value == 2, b.value == 1One word makes it shared
If setting p1.score = 50 makes p2.score print 50 too, look for **static. A static field has a single copy for the whole class**, so every Player sees the same score. Instance fields (no static) are per object.
class Player {
static int score; // shared by ALL!
}In real projects
A web server handles many users at once. A field that should be per-user but was accidentally made static gets shared across every request ā one customer suddenly sees another customer's cart. Per-object state belongs in instance fields.
Key takeaways
- Fields live as long as their object
- Fields default to 0, 0.0, false, '\u0000' or null
- Local variables have no default and must be assigned
- Methods read and change the state of the object they run on
A char field defaults to '\u0000', the "null character". Printing it shows nothing visible, but it's there: (int) of it is 0.
Practice questions
What does this print?
class P {
int n;
boolean b;
String s;
}
void main() {
P p = new P();
System.out.println(p.n + " " + p.b + " " + p.s);
}- 0 false null
- Compile error
- 0 false
- null null null
Check your answer
0 false null. Fields are always initialized to defaults: 0 for int, false for boolean and null for references. Concatenation turns null into "null".
What does this print?
int count;
System.out.println(count);- 0
- null
- Compile error
Check your answer
Compile error. Local variables get no default value. Reading count before assigning it is a compile error: "variable count might not have been initialized".