Sameer Sarmah
Sameer Sarmah

Reputation: 1052

Synchronized block in java

I came across the code:

synchronized(Account.this)   
{}   

where Account is a class.
Does Account.this mean any current instance of class Account?

Upvotes: 1

Views: 62

Answers (2)

OldCurmudgeon
OldCurmudgeon

Reputation: 65793

This would probably be from an inner class of Account.

class Account {
  class InnerAccount {
    ...
    synchronized(Account.this) {
    }
  }
}

Upvotes: 4

Samuel O'Malley
Samuel O'Malley

Reputation: 3541

Normally it is used inside of an inner class: It means the this instance of the outer Account class.

Writing this by itself will return the instance of the inner class, not the outer class.

Upvotes: 1

Related Questions