Java Class isSynthetic()方法及实例

来源:这里教程网 时间:2026-02-17 20:42:09 作者:

Java Class isSynthetic()方法及实例

java.lang.Class类isSynthetic() 方法是用来检查该类是否为合成类。如果该类是合成类,该方法返回true。否则返回false。
语法

public boolean isSynthetic()

参数: 该方法不接受任何参数。

返回值: 如果这个类是合成类,该方法返回 true 。否则返回 false

下面的程序演示了isSynthetic()方法。

例1 :

// Java program to demonstrate isSynthetic() method public class Test {    public static void main(String[] args)        throws ClassNotFoundException    {         // returns the Class object for this class        Class myClass = Class.forName("Test");         System.out.println("Class represented by myClass: "                           + myClass.toString());         // Check if this class is an synthetic        // using isSynthetic() method        System.out.println("Is Test an synthetic: "                           + myClass.isSynthetic());    }}

输出

Class represented by myClass: class TestIs Test an synthetic: false

例2 :

// Java program to demonstrate isSynthetic() method // declaring an Annotation Type@interface A {    // interface element definitions} public class Test {    public static void main(String[] args)        throws ClassNotFoundException    {         // returns the Class object for A        Class myClass = A.class;         // Check if myClass is an synthetic        // using isSynthetic() method        System.out.println("Is A an synthetic: "                           + myClass.isSynthetic());    }}

输出

Is A an synthetic: false

参考资料: https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#isSynthetic-

相关推荐