Sobes.tech
Junior — Middle

What is specified in parentheses when using the synchronized keyword?

sobes.tech AI

Answer from AI

In parentheses after the synchronized keyword, the monitor object on which synchronization will occur is specified. This can be any object, for example, this, another object, or a class (.class).

Example:

synchronized(this) {
    // critical section
}

Or synchronization on a class:

synchronized(MyClass.class) {
    // critical section
}

If synchronized is used in a method declaration, parentheses are not specified, and the monitor is either the current object (this) for non-static methods or the class object for static methods.