Course contentsShow
← Java
Lesson 350 of 2,8708. Classes and EncapsulationFree lesson

Default Values for Uninitialized Fields

Learn what default values Java assigns to instance variables when not explicitly initialized (0, false, null).

Default Values for Uninitialized Fields

What you'll learn: You'll discover exactly what values Java automatically assigns to instance variables when you don't initialize them yourself.

The Safety Net You Didn't Know You Had

When you declare a field in a class but don't give it an initial value, Java doesn't leave it undefined or full of random garbage. Instead, the compiler automatically assigns a predictable default value based on the field's type.

Think of it like moving into a new apartment: even before you bring your own furniture, the landlord has installed basic fixtures—light bulbs, a toilet, a sink. Similarly, Java provides "basic fixtures" for your fields so they're always in a known, safe state.

The Default Value Rules

Here's what Java assigns:

  • Numeric primitives (int, long, double, etc.): 0 (or 0.0 for floating-point)
  • boolean: false
  • char: '\u0000' (the null character)
  • Reference types (objects, arrays, String): null
public class Product {
    private int quantity;        // defaults to 0
    private double price;        // defaults to 0.0
    private boolean inStock;     // defaults to false
    private String name;         // defaults to null
    
    public void display() {
        System.out.println("Quantity: " + quantity);  // prints 0
        System.out.println("Price: " + price);        // prints 0.0
        System.out.println("In stock: " + inStock);   // prints false
        System.out.println("Name: " + name);          // prints null
    }
}

When This Happens

These defaults apply only to instance variables (fields). Local variables inside methods do not get default values—you must initialize them explicitly before use.

Key Takeaway: Java automatically initializes uninitialized instance variables to sensible defaults: 0 for numbers, false for booleans, and null for reference types, ensuring your objects always start in a predictable state.