当前位置: 首页>>代码示例>>用法及示例精选>>正文


Java Modifier isTransient(mod)用法及代码示例

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

用法:

public static boolean isTransient(int mod)

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


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

以下示例程序旨在说明isTransient()方法:
示例1:

// 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

示例2:

// 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://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Modifier.html#isTransient(int)



相关用法

注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Modifier isTransient(mod) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。