2 This instance method synchronizes on <code>this.getClass()</code>. If this class is subclassed,
3 subclasses will synchronize on the class object for the subclass, which isn't likely what was intended.
4 For example, consider this code from java.awt.Label:
6 private static final String base = "label";
7 private static int nameCounter = 0;
8 String constructComponentName() {
9 synchronized (getClass()) {
10 return base + nameCounter++;
14 <p>Subclasses of <code>Label</code> won't synchronize on the same subclass, giving rise to a datarace.
15 Instead, this code should be synchronizing on <code>Label.class</code>
17 private static final String base = "label";
18 private static int nameCounter = 0;
19 String constructComponentName() {
20 synchronized (Label.class) {
21 return base + nameCounter++;
25 <p>Bug pattern contributed by Jason Mehrens</p>