šŸ—ļø Classes & Objects Ā· Intermediate

Fields & state in Java

Instance fields hold each object's state.

🧩 The mysteryCreate a brand-new object and print its fields without ever setting them. Crash? Random garbage? Neither — Java quietly filled them in for you. But try the same with a local variable...

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.

šŸ”® Predict it

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);
}
  1. 0 false null
  2. Compile error
  3. 0 false
Show the answer

0 false null. Fields get defaults automatically, and string concatenation turns the null reference into the text "null".

šŸ”® Predict it

Now a local variable

Locals are declared inside a method. What happens?

int count;
System.out.println(count);
  1. 0
  2. null
  3. Compile 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 == 1
āš ļø The trap

One 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 the real world

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

  1. Fields live as long as their object
  2. Fields default to 0, 0.0, false, '\u0000' or null
  3. Local variables have no default and must be assigned
  4. Methods read and change the state of the object they run on
🤯 Did you know?

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);
}
  1. 0 false null
  2. Compile error
  3. 0 false
  4. 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);
  1. 0
  2. null
  3. 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".

Next: constructors — and the free constructor Java silently takes away from you.