📌  相关文章
📜  Java中的修饰符 isTransient(mod) 方法及示例

📅  最后修改于: 2022-05-13 01:55:34.973000             🧑  作者: Mango

Java中的修饰符 isTransient(mod) 方法及示例

Java.lang.reflect.ModifierisTransient(mod)方法用于检查整数参数是否包含瞬态修饰符。如果此整数参数表示瞬态类型修饰符,则方法返回 true,否则返回 false。

句法:

public static boolean isTransient(int mod)

参数:此方法接受一个整数名称,因为 mod 表示一组修饰符。

返回:如果 mod 包含瞬态修饰符,此方法返回 true;否则为假。

下面的程序说明了 isTransient() 方法:
方案一:

// Java program to illustrate isTransient() method
  
import java.lang.reflect.*;
  
public class GFG {
  
    public static transient int number = 1000;
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // get Field class object
        Field field
            = GFG
                  .class
                  .getDeclaredField("number");
  
        // get Modifier Integer value
        int mod = field.getModifiers();
  
        // check Modifier is transient or not
        boolean result
            = Modifier.isTransient(mod);
  
        System.out.println("Mod integer value "
                           + mod + " is transient : "
                           + result);
    }
}
输出:
Mod integer value 137 is transient : true

方案二:

// Java program to illustrate isTransient()
  
import java.io.Serializable;
import java.lang.reflect.*;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // get Field class object
        Field field
            = GFGTest
                  .class
                  .getDeclaredField("password");
  
        // get Modifier Integer value
        int mod = field.getModifiers();
  
        // check Modifier is transient or not
        boolean result = Modifier.isTransient(mod);
  
        System.out.println("Mod integer value "
                           + mod
                           + " is transient : "
                           + result);
    }
  
    // A sample class that uses a transient keyword to
    // skip their serialization.
    class GFGTest implements Serializable {
  
        // Making password transient for security
        private transient String password;
  
        // other code
    }
}
输出:
Mod integer value 130 is transient : true

参考资料: https: Java/lang/reflect/Modifier.html#isTransient(int)