This piece of code will not compile:
synchronized( obj ) {
Object a = new Object()
}
System.out.println( a.toString() );
Yet I don't know why.. My understanding was that a synchronized block was always eventually executed, so I would expect code following the synchronized block to be aware of any new declared variables. Where am I wrong?
It's not the synchronization, it's the {}
symbols. They define a scope, no matter whether there's an if
, for
, synchronized
, or even nothing at the beginning of them. So the a
goes out of scope once the block finishes, because it was declared within it. (Also there's a missing semicolon at the end of the Object a
declaration but I suspect you just forgot to copy that.)