26

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

 public final class Locale
    implements Cloneable, Serializable
{
    private static class LocaleNameGetter
        implements sun.util.LocaleServiceProviderPool.LocalizedObjectGetter
    {

        public transient String getObject(LocaleNameProvider localenameprovider, Locale locale, String s, Object aobj[])
        {
            if(!$assertionsDisabled && aobj.length != 2)
                throw new AssertionError();
            int i = ((Integer)aobj[0]).intValue();
            String s1 = (String)aobj[1];
            switch(i)
            {
            case 0: // '\0'
                return localenameprovider.getDisplayLanguage(s1, locale);

            case 1: // '\001'
                return localenameprovider.getDisplayCountry(s1, locale);

            case 2: // '\002'
                return localenameprovider.getDisplayVariant(s1, locale);
            }
            if(!$assertionsDisabled)
                throw new AssertionError();
            else
                return null;
        }

Can someone tell me why can this be?

Chuanshi Liu
  • 514
  • 1
  • 7
  • 11
  • 1
    @user85121 can you provide the link where you see that? – Eugene Apr 26 '13 at 10:15
  • 1
    I checked jdk 1.6 source and it doesn't have `transient` keyword – sanbhat Apr 26 '13 at 10:17
  • 4
    Related: http://stackoverflow.com/questions/4936803/why-java-methods-with-varargs-identified-as-transient – harsh Apr 26 '13 at 10:19
  • 1
    @user85121, what version of jdk do you have? This is an important question, in order to check what is going on in the specific source. If this code is the result from a decompiler the author should have mentioned this! – Menelaos Apr 26 '13 at 10:19
  • It isn't in JDK5/6, might have come in between releases or never at all. See http://javasourcecode.org/html/open-source/jdk/jdk-5.0/java/util/Locale.java.html and http://javasourcecode.org/html/open-source/jdk/jdk-6u23/java/util/Locale.java.html – harsh Apr 26 '13 at 10:25
  • 1
    Seems the author disappeared... :( – Menelaos Apr 26 '13 at 10:30

6 Answers6

50

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.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
6

If this code has been decompiled it is most likely a result of this: Why Java methods with varargs identified as transient?

I am quoting from there:

Sort of an answer can be found in the code of javassist AccessFlag

public static final int TRANSIENT = 0x0080; public static final int VARARGS = 0x0080; It appears both have the same values. And since transient means nothing for methods, while varargs means nothing for fields, it is ok for them to be the same.

Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
3

transient can only be applied to member variables and not to methods so there is a problem here.

Looking at the variable names in your code - things like String s and Object[] aboj - it looks like this source has been generated by decompiling the relevant .class file.

I think there is a bug in whichever decompiler you're using which is erroneously adding transisent to the method declaration.

David Webb
  • 190,537
  • 57
  • 313
  • 299
1

This has to be a bug. Or some buggy revision? transient is only applied on variables. Can you provide a link where you see that?

Eugene
  • 117,005
  • 15
  • 201
  • 306
1

Java documentation states that transient keyword is only applied to instance variables so this doesn´t make any sense

Rubén
  • 524
  • 5
  • 22
0
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Boolean  succ;
    private String code;
    private String msg;
    protected T data;

    public Result() {
    }

    public Result(Boolean succ) {
        this.succ = succ;
    }

    
    


    //Can use this here
    @Transient
    public boolean isFail(){
        return !isSucc();
    }
    public boolean isSucc() {
        return succ != null && succ;
    }
}
Jamesqian
  • 1
  • 1
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Suraj Rao Oct 12 '21 at 07:17
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 12 '21 at 07:43