Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can transient keywords mark a method?

Tags:

java

transient

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?

like image 353
Chuanshi Liu Avatar asked Apr 26 '13 10:04

Chuanshi Liu


People also ask

Is it allowed to mark a method as transient?

In case you define any data member as transient, it will not be serialized. This is because every field marked as transient will not be serialized. You can use this transient keyword to indicate the Java virtual machine (JVM) that the transient variable is not part of the persistent state of an object.

What does the transient keyword do?

The transient keyword in Java is used to avoid serialization. If any object of a data structure is defined as a transient , then it will not be serialized. Serialization is the ​process of converting an object into a byte stream.

What are transient methods?

transient is a variables modifier used in serialization. At the time of serialization, if we don't want to save value of a particular variable in a file, then we use transient keyword. When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type.

How does marking a field as transient makes it possible to serialize an object?

transient doesn't turn off serialization generally, but only for the field with which it's associated. Thus, serializing Foo would not transmit a value for the v3 field.


1 Answers

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.

like image 90
Evgeniy Dorofeev Avatar answered Sep 24 '22 12:09

Evgeniy Dorofeev