When you declare an instance field in a Java class without an initializer, what value does it hold, and how does that depend on its type?
answer
- 0 / 0.0 / false / '' / null
- fields default; locals do NOT
- locals need definite assignment (compile error)
- Integer field → null, int field → 0
- JVM zeroes memory at allocation
basics
~10 sUninitialized fields get a default automatically: 0 for number types, false for boolean, '' (the null character) for char, and null for any object/reference type. You don't have to set them yourself.
solid answer
~40 sInstance and static fields that you declare without an initializer are automatically zero-initialized by the JVM. The exact value depends on the type: numeric primitives (byte, short, int, long) default to 0, floating-point types (float, double) to 0.0, char to '' (code point 0, the null character), and boolean to false. Every reference type (objects, arrays, String) defaults to null. This happens during object construction, before your constructor body runs, so a field is never in an undefined state. The same defaulting applies to array elements after the array is created. Local variables are the exception: they get no default and must be definitely assigned before use, or the code won't compile.
go deeper
Recall the table: 0 for numbers, false for boolean, '' for char, null for references; and that fields get these automatically while locals don't.
Explain that the JVM zeroes memory and that defaults apply to instance fields, static fields, and array elements; distinguish primitive vs reference defaults including the wrapper-null trap.
Tie it to definite-assignment analysis for locals, the object initialization order (defaults → initializers → constructor), and why the field-vs-local distinction is a deliberate safety design.
Discuss implications for API design and safe object construction (avoid leaking 'this' during construction so others don't observe default-state fields), and the spec guarantees the JVM relies on.
## What a 'field' is In Java a **field** (also called a member variable) is a variable declared directly inside a class but outside any method, like `int count;` or `String name;`. There are two kinds: **instance fields** (one copy per object) and **static fields** (one copy shared by the whole class). This topic is about what value a field holds when you declare it *without* giving it a starting value (no `= something`). ## The rule: fields are default-initialized Unlike some languages (C/C++) where an uninitialized variable contains whatever garbage was previously in that memory, Java **guarantees** every field starts with a well-defined default value. The Java Virtual Machine (JVM) zeroes out the memory for an object when it is allocated. So the moment an object exists, all its fields already have predictable values, even before your constructor runs. ## The defaults, by type A **primitive type** is one of Java's built-in value types (not an object). Their defaults are all 'the zero value' of that type: - `byte`, `short`, `int`, `long` → `0` - `float` → `0.0f`, `double` → `0.0d` - `char` → `''` — this is the Unicode character with code point 0, the 'null character'. It is NOT the digit '0' (which is `'0'`), and it is NOT `null` (a char can never be null). If you print it you usually see nothing or a blank, and `(int) myChar` is `0`. - `boolean` → `false` A **reference type** is anything that is not primitive: every object, every array, `String`, your own classes, wrapper types like `Integer`. Their default is always **`null`**, meaning 'points to no object'. Note this catch: a field of type `Integer` (the wrapper class) defaults to `null`, but a field of type `int` (the primitive) defaults to `0` — mixing these up causes `NullPointerException` when the `null` wrapper is auto-unboxed. ## Where these defaults apply 1. **Instance fields** — set when the object is created. 2. **Static fields** — set when the class is first loaded/initialized. 3. **Array elements** — when you create an array with `new int[3]`, all three slots are `0`; `new String[3]` gives three `null` elements. The defaulting is by the array's element type. ## The big exception: local variables A **local variable** is one declared inside a method, constructor, or block. Local variables get **no** default value. The Java compiler enforces a rule called **definite assignment**: you must assign a value to a local variable along every path that reaches a use of it, otherwise you get a compile error: 'variable X might not have been initialized'. This is a deliberate safety feature — accidentally reading an unset local is almost always a bug, so the language catches it at compile time instead of silently handing you a default. ## Why the difference exists Fields belong to objects that may be constructed in many ways and live a long time, so a guaranteed safe initial state avoids undefined behavior. Locals are short-lived and the compiler can prove whether they were set, so it demands you do so explicitly, which prevents subtle bugs. ## Order of initialization for fields When an object is built: (1) memory is zeroed to defaults, (2) field initializers and instance-initializer blocks run in source order, (3) the constructor body runs. So a default is only ever visible if you haven't given the field an explicit value yet.
- What value does the boolean default to, and the char?boolean defaults to false; char defaults to '' (the null character, code point 0), not the digit '0'.
- If a field is Integer (wrapper) rather than int, what's its default and what risk does that create?It defaults to null. Using it in arithmetic auto-unboxes it, throwing NullPointerException.
saying these in an interview costs you the question
- Saying char defaults to '0' the digit — it's '', the null character (code point 0)
- Claiming local variables also get default values — they require explicit assignment
- Saying a char can be null — char is a primitive and is never null
- Thinking an Integer/wrapper field defaults to 0 — it defaults to null