2

Essential Java Generics states that the output for this code:

public interface Foo<T> {
    void foo(T param);
}

public class Bar implements Foo<Bar> {
    // This method will appear twice once with Object as parameter and once with Bar.
    public void foo(Bar param) {
    }

    public static void main(String[] args) {
        for (Method m : Bar.class.getMethods())
            if (m.getName().startsWith("foo"))
                System.out.println(m.toGenericString());
    }
}

is:

public void Bar.foo(Bar)
public volatile void Bar.foo(java.lang.Object)

But when I compiled (1.7 compiler) and run the code, I have:

public void Bar.foo(Bar)
public void Bar.foo(java.lang.Object)

I re-compiled it using a 1.6 compiler. But my output is still the same.

The author's exact quote is:

$ java Bar
public void Bar.foo(Bar)
public volatile void Bar.foo(java.lang.Object)

So it doesn't seem like a typo error on his part.

What is causing the output to be different?

Pacerier
  • 86,231
  • 106
  • 366
  • 634

1 Answers1

7

There is no such thing as a volatile method in Java. Only fields can be volatile.

See Why make a method volatile in java? for further details.

As the top answer there explains, the same bit mask (0x00000040) is used for different purposes for fields and for methods. This confuses some older Java tools, causing them to incorrectly mark bridge methods as volatile.

In other words, there was a bug in the software that the blog writer was using at the time, causing the software to mistakenly include the volatile in the printout. The bug got fixed, and you're not seeing the erroneous modifier anymore.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Are you saying that the writer of that blogpost made a mistake? – Pacerier May 09 '12 at 10:26
  • @Pacerier: What I am saying is that in this context, `volatile` is a red herring. – NPE May 09 '12 at 10:28
  • Ok just to make sure I understand you, you are saying that the writer made a typo mistake as the output would never have had been `public volatile void Bar.foo(java.lang.Object)` ? – Pacerier May 09 '12 at 10:32
  • 1
    @Pacerier: I've expanded the answer in an attempt to make it clearer. The method in question is what's known as a bridge method, and some tools are a bit buggy in that they display bridge methods as `volatile` (again, there's no such thing as a volatile method). – NPE May 09 '12 at 10:38