Search code examples
javamethodsvolatile

'volatile' in method signature?


This one is weird. I have the following code:

class A
{   
    protected A clone() throws CloneNotSupportedException
    {
        return (A) super.clone();       
    }
}

when I de-compiled its bytecode through 'showmycode.com', it showed me the following code:

class A
{

    A()
    {
    }

    protected A clone()
    throws clonenotsupportedexception
    {
        return (A)super.clone();
    }

    protected volatile object clone()
    throws clonenotsupportedexception
    {
        return clone();
    }
}

What does it mean for a method return type to be volatile in the second 'clone' method? (This code was compiled through Eclipse's default JDK 1.6 compiler).


Solution

  • The modifier mask for fields and methods is similar but not exactly the same. The decompiler is most likely using the toString method here

    http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/reflect/Modifier.java

    but what it doesn't do is handle all bits

    // Bits not (yet) exposed in the public API either because they
    // have different meanings for fields and methods and there is no
    // way to distinguish between the two in this class, or because
    // they are not Java programming language keywords
    

    What its doesn't handle is the bits which can mean synthetic and bridge which identify compiler generated code.

    If volatile means anything at all here, it could mean don't remove the method even though it doesn't do anything.