Adrian Crum wrote:
> -----------------------------------------------
> In Adam's example, it is entirely possible that
> 
> object = new Result(foo, bar);
> 
> will be executed before
> 
> Object foo = getFoo();
> Object bar = getBar();
> 
> -----------------------------------------------

To restate this:

original:
=====
Object foo = getFoo();
Object bar = getBar();
object = new Result(foo, bar);

class Result {
  Result(Object foo, Object bar) {
    this.foo = foo;
    this.bar = bar;
  }
  String doSomething() {
    return foo.toString();
  }
}
=====

jvm optimized:
=====
object = [new memory block with type of Result]
object.bar = getBar()
object.foo = getFoo()
=====

Note how object is no longer null after the first commmand, so the
outer check will think everything is happy.

The outer code could even call doSomething on the uninitialized block,
which then tries to dereference foo, which hasn't been assigned to yet.

Reply via email to