當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Modifier isFinal(mod)用法及代碼示例


java.lang.reflect.Modifier的isFinal(mod)方法用於檢查integer參數是否包含final修飾符。如果此整數參數表示最終類型Modifier,則方法返回true,否則返回false。

用法:

public static boolean isFinal(int mod)

參數:此方法接受整數名稱,因為mod代表一組修飾符。


返回:如果mod包含final修飾符,則此方法返回true;否則,返回false。否則為假。

以下示例程序旨在說明isFinal()方法:
示例1:

// Java program to illustrate isFinal() method 
  
import java.lang.reflect.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // get Method class object 
        Method[] methods 
            = Shape.class.getMethods(); 
  
        // get Modifier Integer value 
        int mod = methods[0].getModifiers(); 
  
        // check Modifier is final or not 
        boolean result = Modifier.isFinal(mod); 
  
        System.out.println("Mod integer value "
                           + mod + " is final : "
                           + result); 
    } 
  
    class Shape { 
  
        public final void drawShape() {} 
    } 
}
輸出:
Mod integer value 17 is final : true

示例2:

// Java program to illustrate isFinal() 
  
import java.lang.reflect.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // get Method class object 
        Method[] methods 
            = Numbers.class.getMethods(); 
  
        // get Modifier Integer value 
        int mod1 = methods[0].getModifiers(); 
        int mod2 = methods[1].getModifiers(); 
  
        // check Modifiers are final or not 
        boolean result1 = Modifier.isFinal(mod1); 
        boolean result2 = Modifier.isFinal(mod2); 
  
        // print results 
        System.out.println("Mod integer value "
                           + mod1 + " for method "
                           + methods[0].getName() 
                           + " is final : "
                           + result1); 
  
        System.out.println("Mod integer value "
                           + mod2 + " for method"
                           + methods[1].getName() 
                           + " is final : "
                           + result2); 
    } 
  
    // sample final class 
    abstract class Numbers { 
  
        public abstract int initializeNumber(); 
        public final int declareNumbers() 
        { 
            return 0; 
        } 
    } 
}
輸出:
Mod integer value 1025 for method initializeNumber is final : false
Mod integer value 17 for methoddeclareNumbers is final : true

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Modifier.html#isFinal(int)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Modifier isFinal(mod) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。