1

I came across the code:

synchronized(Account.this)   
{}   

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

Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
Sameer Sarmah
  • 1,052
  • 4
  • 15
  • 33
  • 2
    Are you sure "Account" is a class and not a poorly named variable? – Jeanne Boyarsky Oct 30 '13 at 00:33
  • `Account.this` is referring to an instance of `Account`, in particular, "this" instance of the `Account`. It may be that the reference is being made from within in an inner class where it's not possible to use `this` directly... – MadProgrammer Oct 30 '13 at 00:35
  • I imagine the code is in an anonymous or otherwise inner class and this the lock is on the outer class. – Boris the Spider Oct 30 '13 at 00:35
  • This explanation of the `Class.this` syntax might be relevant: http://stackoverflow.com/a/5530293/2288659 – Silvio Mayolo Oct 30 '13 at 00:36
  • This is a question not about multithreading but rather about Java syntax. I don't think the latter two tags are appropriate. – arshajii Oct 30 '13 at 00:36

2 Answers2

4

This would probably be from an inner class of Account.

class Account {
  class InnerAccount {
    ...
    synchronized(Account.this) {
    }
  }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
1

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.

Samuel O'Malley
  • 3,471
  • 1
  • 23
  • 41