On 2/7/2011 11:59 PM, Niclas Hedhman wrote:
On Tue, Feb 8, 2011 at 12:00 PM, Patricia Shanahan<p...@acm.org>  wrote:

The synchronization is ineffective, because getClass() can return different
Class objects in a non-final class' constructor.

This is important news to me... Are you sure about that? Got any JLS
reference/pointer?

It is a logical consequence of two basic facts. If you doubt either or both of them, tell me and I'll dig up the references:

1. There is an explicit or implicit call to a superclass constructor at the start of any constructor that does not begin with a "this" call, other than the Object constructor.

2. getClass() returns a reference to the class object for the method invocation target, not the class object for the class containing the call to getClass().

Here is a trivial program illustrating the effect:

public class GetClassTest {
  public static void main(String[] args) {
    new GetClassTest();
    new Sub1();
    new Sub2();
    new SubSub();
  }

  GetClassTest() {
    System.out.printf(
      "Class object toString \"%s\", Class object hashcode %d%n",
        getClass(),
        getClass().hashCode());
  }
}

class Sub1 extends GetClassTest {
}

class Sub2 extends GetClassTest {
}

class SubSub extends Sub1 {
}


The output is:

Class object toString "class GetClassTest", Class object hashcode 2106235183
Class object toString "class Sub1", Class object hashcode 729943514
Class object toString "class Sub2", Class object hashcode 646414701
Class object toString "class SubSub", Class object hashcode 2131949076

Patricia

Reply via email to