Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What lock does each of these use, and what guarantees does synchronized give beyond mutual exclusion? (1) a synchronized instance method, (2) a

Java
synchronized static method, (3) synchronized (lockObject) { ... }

A synchronized instance method locks the object it is called on (this), a synchronized static method locks the Class object of its class, and a synchronized (lockObject) block locks lockObject. Only one thread at a time can hold a given monitor, and beyond that mutual exclusion, synchronized gives visibility: releasing a monitor happens-before every later acquisition of the same monitor, so the next holder sees all writes made inside. The lock is released even when the block exits with an exception, and because instance and static methods use different locks, they do not exclude each other.

Why this question is asked

synchronized is the first concurrency tool every Java developer learns, and most of its bugs come from one misunderstanding: which object is actually being locked. Two methods that both say synchronized may not protect each other at all. Interviewers ask this to check that we know the lock behind each form and that synchronized is about more than one thread at a time, since it also makes changes visible between threads. In this answer, we will recap what a monitor is, look at the lock behind each of the three forms, see the visibility guarantee in action, check what happens on an exception, and finish with the classic static versus instance trap.

1A Quick Recap of Monitors

Every Java object has a monitor, a lock built into the object that the JVM manages for us. The synchronized keyword is how we use it: a thread entering a synchronized method or block must first acquire the monitor of a particular object, and it releases the monitor when it leaves. If another thread already holds that monitor, the newcomer blocks until it is free.

The key word is particular. synchronized never locks a method or a piece of code; it always locks an object, and two pieces of code exclude each other only if they lock the same object.

2The Lock Behind Each of the Three Forms

Here are the three forms from the question side by side, with the lock each one acquires written next to it. The first two forms are shorthand: the compiler marks the method with a flag, and the JVM picks the lock implicitly.

Java
class Account {
private final Object lockObject = new Object();
private static int created;
private int balance;
synchronized void deposit(int x) { balance += x; } // lock: this (the Account instance)
static synchronized void countNew() { created++; } // lock: Account.class
void withdraw(int x) {
synchronized (lockObject) { balance -= x; } // lock: lockObject
}
}
// The first two are equivalent to:
void deposit2(int x) { synchronized (this) { balance += x; } }
static void countNew2() { synchronized (Account.class) { created++; } }
Which object each form locks
FormLock acquiredExcludes
synchronized instance methodthisother code locking the same instance
static synchronized methodthe Class object (Account.class)other code locking Account.class
synchronized (lockObject) { }lockObjectother code locking that same object

Notice a problem in this example class on purpose: deposit locks this, but withdraw locks lockObject. Both change balance, yet they use different locks, so a deposit and a withdrawal can run at the same time and lose an update. A lock protects data only if every access to that data uses the same lock.

3Beyond Mutual Exclusion: Visibility

Mutual exclusion stops two threads from being inside at the same time. But threads can also fail to see each other's writes: the CPU and the JIT may keep a value in a register or a core's cache, or reorder writes, so a write made by thread A may not be visible to thread B for a long time, or ever.

synchronized solves this through the Java Memory Model's happens-before rule: an unlock of a monitor happens-before every later lock of the same monitor. In plain words, everything thread A wrote before leaving the synchronized block is guaranteed to be visible to thread B after B enters a block on the same object.

Java
class Flag {
private boolean ready; // not volatile
private int value;
synchronized void publish() { value = 42; ready = true; } // writes, then unlock
synchronized int readIfReady() { return ready ? value : -1; } // lock, then reads
}
// thread A: flag.publish();
// thread B, later: flag.readIfReady(); // 42, never a stale 0 once ready is seen

Notice that the guarantee only holds when both threads use the same monitor. If readIfReady were not synchronized, B could see ready as true and still read a stale value, or never see ready at all. That is why reads need the lock too, not only writes.

4The Lock Is Released on Exceptions Too

When a synchronized block or method exits by throwing an exception, the JVM releases the monitor before the exception propagates. For a synchronized block, the compiler generates a hidden exception handler that runs monitorexit; for a synchronized method, the JVM does it as part of the method return. So we never write finally to unlock a monitor, unlike with ReentrantLock, where forgetting unlock() in finally leaves the lock held forever.

But there is a catch: releasing the lock does not undo the changes. If the block updated two fields and threw between them, the next thread sees the first change without the second. We still have to keep the object's state consistent ourselves, for example by computing new values first and assigning them at the end.

5Static and Instance Methods Do Not Exclude Each Other

Because a static synchronized method locks the Class object and an instance synchronized method locks the instance, a thread inside one does not block a thread entering the other. They are different objects with different monitors.

This matters when both methods touch the same data. If an instance method updates a static counter while a static method also updates it, each holds a different lock and the updates race. The fix is to protect the shared static data with one lock, for example by making all its accessors static synchronized, or by locking Account.class or a private static final lock object everywhere it is used.

6Follow-up questions

Is a synchronized method slower than a synchronized block?

Not in a way that matters. Both acquire a monitor the same way. Blocks are preferred because they let us lock a private object instead of this and keep the locked region small, holding the lock only around the shared state and not around slow work such as I/O.

Does synchronized on one instance block threads using another instance?

No. Two Account objects have two monitors, so a thread depositing into account 1 never waits for a thread depositing into account 2. That is usually what we want, as long as the data being protected belongs to the instance and not to the class.

Is volatile a replacement for synchronized?

Only for visibility of a single variable. volatile guarantees that writes are seen by later reads, but it gives no mutual exclusion, so balance += x on a volatile field can still lose updates, because the read, add and write are separate steps.

7Conclusion

In this answer, we saw that synchronized always locks an object: this for an instance method, the Class object for a static method, and the named object for a block. Only one thread can hold a given monitor, and an unlock happens-before the next lock of the same monitor, so writes made inside become visible to the next holder. The monitor is released even when an exception escapes, though changes are not rolled back, and static and instance methods lock different objects, so they do not protect each other.

What to say in the interviewAbout 80 seconds aloud

Open with

synchronized always locks an object, never code. A synchronized instance method locks this, the instance it is called on. A static synchronized method locks the Class object, for example Account.class.

Then, if they want more

And a synchronized block locks whatever object we put in the parentheses. Only one thread can hold a given monitor at a time, so code that locks the same object is mutually exclusive, but code that locks different objects is not. Beyond mutual exclusion, synchronized gives visibility.

Under the Java Memory Model, releasing a monitor happens-before every later acquisition of the same monitor, so whatever a thread wrote inside the block is guaranteed to be seen by the next thread that locks the same object. That is also why reads need the lock, not only writes.

The JVM releases the monitor even if the block exits with an exception, so we never need a finally to unlock it, although the changes made before the exception stay. And one trap: static and instance synchronized methods use different locks, the class and the instance, so they do not exclude each other.

If both touch a static field, I would protect it with one lock, such as a private static final lock object.

About this answer

Last updated
12 November 2026
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 "What lock does each of these use, and what guarantees does synchronized give beyond...": https://scrivio-interview-library.netlify.app/java/synchronization/f362c5b88e/

More in Synchronization and monitors