Scrivio Interview library
ConceptPrincipal6 min read · updated 12 November 2026

Java gave every object a monitor in 1.0. In hindsight, what did that design cost, and what does it mean for the future of the language, for example value-based classes and the value objects proposed by Project Valhalla?

Giving every object a monitor meant every object header must be able to hold lock state, which costs header space and a lot of runtime complexity, and it meant any object can be locked, so code locks on strings, boxed values and public objects and interferes with other code. Java 16 (JEP 390) began warning about synchronizing on value-based classes such as Integer, and Project Valhalla's proposed value objects have no identity, so synchronizing on them is expected to fail; check the current JEP status. The lesson is that a feature available on every object is very hard to take back, and explicit lock objects are the safer habit.

Why this question is asked

Most language decisions from 1995 are invisible today, but this one still shapes how every Java object is laid out in memory and how the JVM's locking code is written. It also explains several modern changes: why the JDK warns about locking an Integer, and why value objects from Project Valhalla cannot be locked. Interviewers ask it at the most senior level to see whether we can reason about a design's long-term cost, not just use it. In this answer, we will recap what the design gave us, look at its cost in memory and in the JVM, then its cost in program correctness, follow the recent steps from value-based classes to Valhalla, and draw the lesson for language and API design.

1What the Design Gave Us

Java 1.0 made every object a potential lock and condition variable: synchronized works on any reference, and wait, notify and notifyAll are methods of Object. It made simple concurrency very easy to start with, because no extra lock object or library was needed, and it fit the idea of an object guarding its own state with synchronized methods.

The price was paid later, and it is paid by every object, including the many billions that are never locked.

2The Cost in the Object Header and the Runtime

Because any object might be locked, every object header must be able to describe a lock state. In HotSpot that is the mark word, shared with the identity hash code and garbage collector bits. Lock states have to be squeezed into a few bits and, when a lock is inflated, the header must point to a separate monitor structure, displacing the hash code it used to hold. The JVM has many careful code paths to keep hash codes, GC information and lock states consistent.

This is one reason headers are 12 to 16 bytes on 64-bit JVMs, a large overhead for small objects such as a Point with two ints. Recent work on compact object headers, which shrinks the header to 8 bytes, has to move monitor information out of the header into a side table, and much of the difficulty of that work comes from the every-object-is-a-lock rule. Biased locking, stack locking, lightweight locking and monitor deflation all exist to make this universal feature cheap, at the cost of years of engineering.

Step throughWhat lives in a 64-bit object header (simplified)
  1. Mark word (8 bytes)identity hash, GC age, and lock state or monitor pointer
  2. Class pointer (4 to 8 bytes)which class this object is
  3. Fieldsthe object's own data

3 layers, from the top down. Step through them one at a time.

The lock state competes for space with the hash code and GC bits in every object.

3The Cost in Correctness: Anything Can Be Locked

The second cost is that the language cannot stop us from locking objects we do not own. Strings are interned and shared across the JVM, so synchronized ("orders") shares a lock with any code using that literal. Boxed values are cached, so synchronized on Integer.valueOf(1) or Boolean.FALSE locks a JVM-wide singleton. A public object, or this in a public class, can be locked by any caller, which is how library and client code end up sharing locks and deadlocking.

If locks had to be explicit objects, as in many later designs, these bugs would not be expressible. Instead, Java needs lint warnings, static analysis rules and guidelines to discourage things the language allows.

4Value-Based Classes and JEP 390

The first formal step back came in Java 16. JEP 390 documented the primitive wrapper classes, such as Integer, Double and Boolean, and some others like Optional and the java.time classes, as value-based: classes whose instances should be treated only as values, without relying on their identity. Their constructors were deprecated for removal, and synchronizing on an instance became something the tools warn about.

Java
Integer key = 42;
synchronized (key) { // javac -Xlint:synchronization:
... // attempt to synchronize on an instance of a value-based class
}
// java -XX:DiagnoseSyncOnValueBasedClasses=1 -> fatal error when this runs
// java -XX:DiagnoseSyncOnValueBasedClasses=2 -> logs a warning when this runs

Notice the shape of the change: warnings first, a diagnostic flag to find occurrences at runtime, and no break yet. That is how the platform prepares millions of lines of code for a future change.

5Value Objects in Project Valhalla

Project Valhalla proposes value classes (JEP 401, in preview work at the time of writing; check the current status): classes whose instances have no identity. Two value objects with the same fields are the same value, == compares their contents, and the JVM is free to copy them, flatten them into arrays and fields, or keep them in registers without a header. Without identity there is no single object to own a monitor, so synchronizing on a value object is expected to be rejected: a compile-time error where the compiler can see it, and an exception at runtime otherwise.

Valhalla also plans to migrate classes like Integer to value classes, which is why JEP 390 started the warnings years earlier. Code that locks on a boxed value today is exactly the code that would break.

6The Lesson: Universal Features Are Hard to Take Back

The broader lesson is about design. A feature offered on every object, such as a monitor, an identity hash code, or mutability by default, becomes something code everywhere may depend on, and removing it later takes a decade of warnings, deprecations and careful migration. A feature offered explicitly, such as a lock object you must create, costs a line of code but can evolve freely.

For our own code, the practical habit follows directly: lock on private final Object instances or java.util.concurrent locks, never on strings, boxed values, Class objects or objects we do not own. That code is correct today and is already ready for value classes.

7Follow-up questions

Would Java be better without wait and notify on Object?

Most likely. java.util.concurrent showed that conditions work better as explicit objects tied to an explicit lock, with several conditions per lock. Putting wait and notify on Object made every class carry methods that almost nobody should call directly.

Will synchronized itself be removed?

No. synchronized on ordinary identity objects remains fully supported and has even been improved, for example so it no longer pins virtual threads. The change affects only objects without identity, such as future value objects.

How would you find code that locks on value-based classes today?

Compile with -Xlint:synchronization, enable static analysis rules for locking on boxed types and strings, and run tests with -XX:DiagnoseSyncOnValueBasedClasses=2 so occurrences that the compiler cannot see, for example through a generic type, are logged at runtime.

8Conclusion

In this answer, we saw that giving every object a monitor made simple locking easy but cost header space and a great deal of JVM complexity, and let code lock on strings, boxed values and public objects that it does not own. JEP 390 in Java 16 marked the wrappers as value-based and started warning about synchronizing on them, and Project Valhalla's value objects, which have no identity, are expected to reject synchronization altogether. The lesson is that universal features are very hard to take back, so explicit lock objects are the safer habit.

What to say in the interviewAbout 90 seconds aloud

Open with

Putting a monitor on every object made the first steps of concurrency very easy, but it had two long-term costs. In the runtime, every object header has to be able to describe lock state, so the mark word shares its bits between the hash code, GC age and the lock, and HotSpot needs biased locking, stack locking, lightweight locking, inflation and deflation just to make a feature cheap that most objects never use.

Then, if they want more

It is also one of the hardest parts of shrinking object headers. In correctness, the language lets us lock anything, so people lock on interned strings, cached boxed values and public objects, sharing locks with code they do not know about, and we need lint and static analysis to discourage it. The platform is now walking this back carefully.

In Java 16, JEP 390 marked the wrapper classes and others as value-based, deprecated their constructors and added warnings and a runtime diagnostic flag for synchronizing on them. Project Valhalla's value objects have no identity, so there is no monitor, and synchronizing on them is expected to fail; I would check the current JEP status for details.

My lesson is that a feature available on every object is very hard to remove, and in my own code I always lock on private final lock objects.

About this answer

Last updated
12 November 2026
Versions it names
Java 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Java gave every object a monitor in 1.0. In hindsight, what did that design cost, and...": https://scrivio-interview-library.netlify.app/java/synchronization/f1c7852237/