Initialization and object identity
What you will learn
Initialization gives a new object its starting state. Object identity answers whether two references point to the same instance, independently of whether the fields contain equal values. Understanding both prevents accidental sharing and partially initialized state.
class Sample { static int announce(String text, int value) { System.out.println(text); return value; } static int classValue = announce("class", 1); int first = announce("field", 2); { System.out.println("block"); } Sample() { System.out.println("constructor"); }}public class Main { public static void main(String[] args) { new Sample(); new Sample(); }}Expected output:
classfieldblockconstructorfieldblockconstructorStatic initialization occurs once in this program; instance initialization repeats for every object.
How it works
For the simple classes in this lesson (no custom superclass), object fields first receive default values. After superclass construction, instance field initializers and instance initializer blocks execute in source order, then the remaining constructor body executes for the conventional Java 21-compatible form. An initializer block is a bare block inside a class body. Constructor delegation does not repeat those initializers. Static initialization is separate: class-level initializers run during class initialization, generally before the first active use such as creating an instance or invoking a static method. Compile-time constant access is an important exception and does not necessarily trigger initialization. Java 25 permits restricted constructor prologues; the examples use the older portable ordering and do not claim all constructor statements must follow superclass initialization in newer Java. Reference equality a == b asks whether the references identify the same object (or both are null). Two new expressions can construct objects with equal field values but distinct identity. Assignment copies references. final Book b prevents changing that variable to refer to another object, but does not freeze the Book. For logical value equality, compare the relevant fields explicitly for now; equals/hashCode contracts appear later. Avoid calling overridable methods from constructors because subclass state may not yet be initialized.
class Label { private int number; Label(int number) { this.number = number; } int number() { return number; } void setNumber(int number) { this.number = number; }}public class Main { public static void main(String[] args) { Label a = new Label(7); Label b = new Label(7); Label alias = a; System.out.println(a == b); System.out.println(a.number() == b.number()); a.setNumber(9); System.out.println(alias.number()); System.out.println(b.number()); }}Expected output:
falsetrue97Equal numbers do not make objects identical. The alias sees mutation of the original object.
Check an edge case
class Note { int revision = 1;}public class Main { public static void main(String[] args) { final Note note = new Note(); note.revision = 2; System.out.println(note.revision); }}Expected output:
2The reference cannot be reassigned, but the object is still mutable.
Common mistakes
Do not use == to test whether independently constructed inventory objects have the same business value. Also do not assume final on a reference prevents its object’s fields from changing. A copied reference is not a snapshot.
Practice
Implement a Label class with a private int number, a constructor, number(), and setNumber(int). Construct two labels with equal numbers and one alias. Verify identity before changing the first object, then show that only its alias observes the change.
Save each complete example as Main.java in a fresh directory, compile with javac --release 21 Main.java, then run java Main. Each example is independent. A local JDK is required; the ByteMind Java runner is not verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Initialization and object identity quiz
Check understanding using Java 21-compatible examples and the stated assumptions.
Two new Label(7) expressions succeed. What is a == b?
When do instance field initializers run in the conventional example?
What does final Label a = new Label(1) prevent?
What is printed first in the Sample initialization example?
Does this(...) constructor delegation initialize instance fields twice?