Can transient keywords mark a method?

前端 未结 5 978
小鲜肉
小鲜肉 2021-02-01 17:45

In a java class java.util.Locale, I find that the keyword transient marked a method.

 public final class Locale
    implements Cloneable, Serializable
{
    priv         


        
5条回答
  •  旧巷少年郎
    2021-02-01 17:59

    No it can't, it's only valid for fields. You seem to get your source from .class by decompiling. This is the decompiler bug, if you take a look at java.lang.reflect.Modifier src you will see that transient and varargs have the same value

    public static final int TRANSIENT        = 0x00000080;
    ...
    static final int VARARGS   = 0x00000080;
    

    for a field 0x00000080 means transient, for a method (your case) it means varargs. This is how getObject looks like in java.util.Locale src

    public String getObject(LocaleNameProvider localeNameProvider,
                            Locale locale, 
                            String key,
                            Object... params) {   <-- varargs
    

    In .class (bytecode) varargs is represented by Object[] as the last parameter + modifier bit 7 = 1 (0x80). I guess the decompiler is old and simply does not know about varargs which is since Java 1.5 so it printed it as transient.

提交回复
热议问题