📜  Java.lang.Class Java中的类 |设置 2

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

Java.lang.Class Java中的类 |设置 2

Java.lang.Class Java中的类 |设置 1

更多方法:

1. int getModifiers() :此方法返回此类或接口的Java语言修饰符,以整数编码。修饰符由Java虚拟机的 public、protected、private、final、static、abstract 和 interface 常量组成。这些修饰符已经在Java.lang.Reflect 包的 Modifier 类中解码。

Syntax : 
public int getModifiers()
Parameters : 
NA
Returns :
the int representing the modifiers for this class
Java
// Java program to demonstrate getModifiers() method
 
import java.lang.reflect.Modifier;
 
public abstract class Test
{
    public static void main(String[] args)
    {
        // returns the Class object associated with Test class
        Class c = Test.class;
 
        // returns the Modifiers of the class Test
        // getModifiers method
        int i = c.getModifiers();
         
        System.out.println(i);
         
        System.out.print("Modifiers of " + c.getName() + " class are : ");
 
        // getting decoded i using toString() method
        // of Modifier class
        System.out.println(Modifier.toString(i));
    }
}


Java
// Java program to demonstrate getEnumConstants() method
 
enum Color
{
    RED, GREEN, BLUE;
}
  
public class Test
{
    public static void main(String[] args)
    {  
        // returns the Class object associated with Color(an enum class)
        Class c1 = Color.class;
         
        // returns the Class object associated with Test class
        Class c2 = Test.class;
         
        // returns the elements of Color enum class in an array
        // getEnumConstants method
        Object[] obj1 = c1.getEnumConstants();
         
        System.out.println("Enum constants of " + c1.getName() + " class are :");
         
        // iterating through enum constants
        for (Object object : obj1)
        {
            System.out.println(object);
        }
         
        // returns null as Test Class object does not represent an enum type
        Object[] obj2 = c2.getEnumConstants();
 
        System.out.println("Test class does not contain any Enum constant.");
        System.out.println(obj2);
         
    }
}


Java
// Java program to demonstrate getCanonicalName() method
public class Test
{
    public static void main(String[] args)
            throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.String");
 
        System.out.print("Canonical name of class represented by c1 : ");
         
        // returns the Canonical name of the class
        // getCanonicalName method
        System.out.println(c1.getCanonicalName());
    }
}


Java
// Java program to demonstrate desiredAssertionStatus() method
public class Test
{
    public static void main(String[] args)
                         throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.String");
         
        // checking for assertion status of String class
         
        System.out.print("desired assertion status of " + c1.getName() + "class: ");
         
        // desiredAssertionStatus() method
        System.out.println(c1.desiredAssertionStatus());
         
    }
}


Java
// Java program to demonstrate getComponentType() method
public class Test
{
    public static void main(String[] args)
    {
        int a[] = new int[2];
         
        // returns the Class object for array class
        Class c = a.getClass();
         
        System.out.print("Component type of class represented by c : ");
         
        // getComponentType() method
        System.out.println(c.getComponentType());
         
    }
}


Java
// Java program to demonstrate getDeclaredClasses() method
 
public class Test
{
    // base interface
    interface A
    {
        // methods and constant declarations
    }
      
    // derived class
    class B implements A
    {
        // methods implementations that were declared in A
    }
 
     
    public static void main(String[] args)
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // getDeclaredClasses on myClass
        // it returns array of classes and interface declare in Test class
        Class c[] = myClass.getDeclaredClasses();
         
        System.out.println("Declared classes and interfaces present in " +
                                                  myClass.getName() + " class : ");
         
        // iterating through classes and interfaces declared in Test class
        for (Class class1 : c)
        {
            System.out.println(class1);
        }
         
    }
}


Java
// Java program to demonstrate getDeclaredField() method
 
import java.lang.reflect.Field;
 
public class Test
{
    // any declared field
    int i;
     
    public static void main(String[] args)
            throws NoSuchFieldException, SecurityException
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // getDeclaredField on myClass
        Field f = myClass.getDeclaredField("i");
         
        System.out.println("Declared field present in " +
                                     myClass.getName() +
                                     " class specified by \"i\" : ");
         
        System.out.println(f);
    }
}


Java
// Java program to demonstrate getDeclaredFields() method
 
import java.lang.reflect.Field;
 
public class Test
{
    // some declared fields
    int i;
    String str;
    boolean b;
     
    public static void main(String[] args)
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // getDeclaredFields on myClass
        Field f[] = myClass.getDeclaredFields();
         
        System.out.println("Declared fields present in " +
                                            myClass.getName() + " class are : ");
         
        // iterating through declared fields of Test class
        for (Field field : f)
        {
            System.out.println(field);
        }
    }
}


Java
// Java program to demonstrate getDeclaredMethod() method
 
import java.lang.reflect.Method;
 
public class Test
{
    // any declared method
    // with a String argument
    public void m1(String str)
    {
        System.out.println(str);
    }
     
    public static void main(String[] args)
            throws NoSuchMethodException, SecurityException, ClassNotFoundException
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // returns the Class object for the class
        // with the specified name
        Class c = Class.forName("java.lang.String");
         
        // getDeclaredMethod on myClass
        Method m = myClass.getDeclaredMethod("m1",c);
         
        System.out.println("Declared method present in " +
                                myClass.getName() +
                                " class specified by argument : " + c.getName());
         
        System.out.println(m);
    }
}


Java
// Java program to demonstrate getDeclaredMethods() method
 
import java.lang.reflect.Method;
 
public class Test
{
    // some declared Methods
    public void m1()
    {
        System.out.println("Inside m1 method");
    }
     
    static void m2()
    {
        System.out.println("Inside m2 method");
    }
     
    // main method
    public static void main(String[] args)
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // getDeclaredMethods on myClass
        Method m[] = myClass.getDeclaredMethods();
         
        System.out.println("Declared methods present in " +
                                            myClass.getName() + " class are : ");
         
        // iterating through declared Methods of Test class
        for (Method Method : m)
        {
            System.out.println(Method);
        }
    }
}


Java
// Java program to demonstrate getDeclaredConstructor() Constructor
 
import java.lang.reflect.Constructor;
 
public class Test
{
     
    public static void main(String[] args)
            throws NoSuchMethodException, SecurityException, ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.Integer");
        Class c2 = Class.forName("java.lang.String");
         
        // getDeclaredConstructor on myClass
        Constructor con = c1.getDeclaredConstructor(c2);
         
        System.out.println("Declared Constructor present in " + c1.getName() +
                                  " class specified by argument : " + c2.getName());
         
        System.out.println(con);
    }
}


Java
// Java program to demonstrate getDeclaredConstructors() Constructor
 
import java.lang.reflect.Constructor;
 
public class Test
{
     
    public static void main(String[] args)
            throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c = Class.forName("java.lang.String");
         
        // getDeclaredConstructors on myClass
        Constructor con[] = c.getDeclaredConstructors();
         
        System.out.println("Declared Constructors present in " +
                                c.getName() + " class are : ");
         
        // iterating through all constructors
        for (Constructor constructor : con)
        {
            System.out.println(constructor);
        }
    }
}


Java
// Java program to demonstrate
// getDeclaringClass() Class
import java.lang.reflect.Method;
public class Test
{
    // any method
    public void m1()
    {
        System.out.println("Inside m1 method");
    }
     
    public static void main(String[] args)
    {
         
        // returns A class object
        Class c1 = Test.class;
         
        // getting all methods of Test class
        // Note that methods from Object class
        // are also inherited
        Method m[] = c1.getMethods();
         
        for (Method method : m)
        {
            // getDeclaringClass method
            // it return declared class of this method
            System.out.println(method.getName() + " is present in "
                                            + method.getDeclaringClass());
        }
         
    }
}


Java
// Java program to demonstrate getEnclosingClass() Class
 
public class Test
{
    // any inner class of Test class
    class A{}
     
    public static void main(String[] args)
    {
         
        // returns A class object
        Class c1 = Test.A.class;
         
        // getEnclosingClass method
        // it returns the class object where A class present
        Class c2 = c1.getEnclosingClass();
         
        System.out.println("The class " + c1.getName() + " is present in class : ");
         
        System.out.println(c2);
         
    }
}


Java
// Java program to demonstrate getEnclosingMethod() method
 
import java.lang.reflect.Method;
 
public class Test
{
    // any method
    public static Class m1()
    {  
        // any local class in m1
        class A{};
         
        // returning class A
        return A.class;
    }
 
     
    // main method
    public static void main(String[] args)
            throws ClassNotFoundException
    {
        // returns A Class object
        Class c = Test.m1();
         
        // getEnclosingMethod method
        Method m = c.getEnclosingMethod();
         
        System.out.println("The class " + c.getName() + " is present in method : ");
         
        System.out.println(m);
         
    }
}


Java
// Java program to demonstrate
// getEnclosingConstructor() Constructor
 
import java.lang.reflect.Constructor;
 
public class Test
{
    Class c;
     
    // default(any) constructor
    public Test()
    {
        // any local class
        class A{};
         
        // returns A class object
        c = A.class;
    }
     
    public static void main(String[] args)
    {
        // creating Test class object
        Test t = new Test();
         
        // getEnclosingConstructor method
        Constructor con = t.c.getEnclosingConstructor();
         
        System.out.println("The class " + t.c.getName()
                        + " is present in constructor : ");
         
        System.out.println(con);
         
    }
}


Java
// Java program to demonstrate getProtectionDomain() method
public class Test
{
    public static void main(String[] args)
                         throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.String");
         
        // checking for assertion status of String class
         
        System.out.println("protection domain of " + c1.getName() + " class : ");
         
        // getProtectionDomain() method
        // it will print null as String class is loaded by
        // BootStrap class loader
        System.out.println(c1.getProtectionDomain());
         
    }
}


输出:

1025
Modifiers of Test class are : public abstract

2. T[] getEnumConstants() :这个方法返回这个枚举类的元素。如果此 Class 对象不表示枚举类型,则返回 null。

Syntax : 
public T[] getEnumConstants()
Parameters : 
NA
Returns :
an array containing the values comprising the enum class represented by this Class object
in the order they're declared,
or null if this Class object does not represent an enum type

Java

// Java program to demonstrate getEnumConstants() method
 
enum Color
{
    RED, GREEN, BLUE;
}
  
public class Test
{
    public static void main(String[] args)
    {  
        // returns the Class object associated with Color(an enum class)
        Class c1 = Color.class;
         
        // returns the Class object associated with Test class
        Class c2 = Test.class;
         
        // returns the elements of Color enum class in an array
        // getEnumConstants method
        Object[] obj1 = c1.getEnumConstants();
         
        System.out.println("Enum constants of " + c1.getName() + " class are :");
         
        // iterating through enum constants
        for (Object object : obj1)
        {
            System.out.println(object);
        }
         
        // returns null as Test Class object does not represent an enum type
        Object[] obj2 = c2.getEnumConstants();
 
        System.out.println("Test class does not contain any Enum constant.");
        System.out.println(obj2);
         
    }
}

输出:

Enum constants of Color class are :
RED
GREEN
BLUE
Test class does not contain any Enum constant.
null

3. String getCanonicalName() :此方法返回Java语言规范定义的基础类的规范名称。
如果基础类没有规范名称(即,如果它是本地或匿名类或组件类型没有规范名称的数组),则返回 null。

Syntax : 
public String getCanonicalName()
Parameters : 
NA
Returns :
the Canonical name of the underlying class, if it exists
null, otherwise

Java

// Java program to demonstrate getCanonicalName() method
public class Test
{
    public static void main(String[] args)
            throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.String");
 
        System.out.print("Canonical name of class represented by c1 : ");
         
        // returns the Canonical name of the class
        // getCanonicalName method
        System.out.println(c1.getCanonicalName());
    }
}

输出:

Canonical name of class represented by c1 : java.lang.String

4. boolean desiredAssertionStatus() :如果在调用此方法时要对其进行初始化,则此方法返回将分配给此类的断言状态。

Syntax : 
public boolean desiredAssertionStatus()
Parameters : 
NA
Returns :
the desired assertion status of the specified class.

Java

// Java program to demonstrate desiredAssertionStatus() method
public class Test
{
    public static void main(String[] args)
                         throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.String");
         
        // checking for assertion status of String class
         
        System.out.print("desired assertion status of " + c1.getName() + "class: ");
         
        // desiredAssertionStatus() method
        System.out.println(c1.desiredAssertionStatus());
         
    }
}

输出:

desired assertion status of java.lang.Stringclass : false

5. Class getComponentType() :该方法返回代表数组组件类型的Class。如果此类不表示数组类,则此方法返回 null。

Syntax : 
public Class getComponentType()
Parameters : 
NA
Returns :
the Class representing the component type of this class if this class is an array

Java

// Java program to demonstrate getComponentType() method
public class Test
{
    public static void main(String[] args)
    {
        int a[] = new int[2];
         
        // returns the Class object for array class
        Class c = a.getClass();
         
        System.out.print("Component type of class represented by c : ");
         
        // getComponentType() method
        System.out.println(c.getComponentType());
         
    }
}

输出:

Component type of class represented by c : int

6. Class[] getDeclaredClasses() :返回一个 Class 对象数组,反映所有声明为该 Class 对象表示的类的成员的类和接口。
此方法包括公共、受保护、默认(包)访问以及该类声明的私有类和接口,但不包括继承的类和接口。如果类没有将类或接口声明为成员,或者如果此 Class 对象表示原始类型、数组类或 void,则此方法返回长度为 0 的数组。

Syntax : 
public Class[] getDeclaredClasses()
Parameters : 
NA
Returns :
the array of Class objects representing all the declared members of this class
Throws :
SecurityException - If a security manager, s, is present 

Java

// Java program to demonstrate getDeclaredClasses() method
 
public class Test
{
    // base interface
    interface A
    {
        // methods and constant declarations
    }
      
    // derived class
    class B implements A
    {
        // methods implementations that were declared in A
    }
 
     
    public static void main(String[] args)
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // getDeclaredClasses on myClass
        // it returns array of classes and interface declare in Test class
        Class c[] = myClass.getDeclaredClasses();
         
        System.out.println("Declared classes and interfaces present in " +
                                                  myClass.getName() + " class : ");
         
        // iterating through classes and interfaces declared in Test class
        for (Class class1 : c)
        {
            System.out.println(class1);
        }
         
    }
}

输出:

Declared classes and interfaces present in Test class : 
interface Test$A
class Test$B

7. Field getDeclaredField(String fieldName) :该方法返回一个Field对象,反映该Class对象所代表的类或接口的指定声明字段。
name 参数是一个字符串,它指定所需字段的简单名称。请注意,此方法不会反映数组类的长度字段。

Syntax : 
public Field getDeclaredField(String fieldName) 
throws NoSuchFieldException,SecurityException
Parameters : 
fieldName -  the field name
Returns :
the Field object for the specified field in this class
Throws :
NoSuchFieldException - if a field with the specified name is not found.
NullPointerException - if fieldName is null
SecurityException - If a security manager, s, is present.

Java

// Java program to demonstrate getDeclaredField() method
 
import java.lang.reflect.Field;
 
public class Test
{
    // any declared field
    int i;
     
    public static void main(String[] args)
            throws NoSuchFieldException, SecurityException
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // getDeclaredField on myClass
        Field f = myClass.getDeclaredField("i");
         
        System.out.println("Declared field present in " +
                                     myClass.getName() +
                                     " class specified by \"i\" : ");
         
        System.out.println(f);
    }
}

输出:

Declared field present in Test class specified by "i" : 
int Test.i

8. Field[] getDeclaredFields() :该方法返回一个Field对象数组,反映了该Class对象所代表的类或接口声明的所有字段。这包括公共、受保护、默认(包)访问和私有字段,但不包括继承的字段。
如果类或接口未声明任何字段,或者此 Class 对象表示原始类型、数组类或 void,则此方法返回长度为 0 的数组。

Syntax : 
public Field[] getDeclaredFields()  throws SecurityException
Parameters : 
NA
Returns :
the array of Field objects representing all the declared fields of this class
Throws :
SecurityException - If a security manager, s, is present.

Java

// Java program to demonstrate getDeclaredFields() method
 
import java.lang.reflect.Field;
 
public class Test
{
    // some declared fields
    int i;
    String str;
    boolean b;
     
    public static void main(String[] args)
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // getDeclaredFields on myClass
        Field f[] = myClass.getDeclaredFields();
         
        System.out.println("Declared fields present in " +
                                            myClass.getName() + " class are : ");
         
        // iterating through declared fields of Test class
        for (Field field : f)
        {
            System.out.println(field);
        }
    }
}

输出:

Declared fields present in Test class are : 
int Test.i
java.lang.String Test.str
boolean Test.b

9. Method getDeclaredMethod(String methodName,Class...parameterTypes) :该方法返回一个Method对象,反映了该Class对象所代表的类或接口的指定声明方法。

Syntax : 
public Method getDeclaredMethod(String methodName,Class... parameterTypes) 
throws NoSuchFieldException,SecurityException
Parameters : 
methodName -  the method name
parameterTypes - the list of parameters
Returns :
the Method object for the method of this class matching the specified name and parameters
Throws :
NoSuchMethodException - if a method with the specified name is not found.
NullPointerException - if methodName is null
SecurityException - If a security manager, s, is present.

Java

// Java program to demonstrate getDeclaredMethod() method
 
import java.lang.reflect.Method;
 
public class Test
{
    // any declared method
    // with a String argument
    public void m1(String str)
    {
        System.out.println(str);
    }
     
    public static void main(String[] args)
            throws NoSuchMethodException, SecurityException, ClassNotFoundException
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // returns the Class object for the class
        // with the specified name
        Class c = Class.forName("java.lang.String");
         
        // getDeclaredMethod on myClass
        Method m = myClass.getDeclaredMethod("m1",c);
         
        System.out.println("Declared method present in " +
                                myClass.getName() +
                                " class specified by argument : " + c.getName());
         
        System.out.println(m);
    }
}

输出:

Declared method present in Test class specified by argument : java.lang.String
public void Test.m1(java.lang.String)

10. Method[] getDeclaredMethods() :该方法返回一个Method对象数组,反映了该Class对象所代表的类或接口所声明的所有方法。这包括公共、受保护、默认(包)访问和私有方法,但不包括继承的方法。
如果类或接口未声明任何方法,或者此 Class 对象表示原始类型、数组类或 void,则此方法返回长度为 0 的数组。

Syntax : 
public Method[] getDeclaredMethods() throws SecurityException
Parameters : 
NA
Returns :
the array of Method objects representing all the declared methods of this class
Throws :
SecurityException - If a security manager, s, is present.

Java

// Java program to demonstrate getDeclaredMethods() method
 
import java.lang.reflect.Method;
 
public class Test
{
    // some declared Methods
    public void m1()
    {
        System.out.println("Inside m1 method");
    }
     
    static void m2()
    {
        System.out.println("Inside m2 method");
    }
     
    // main method
    public static void main(String[] args)
    {
        // returns the Class object associated with Test class
        Class myClass = Test.class;
         
        // getDeclaredMethods on myClass
        Method m[] = myClass.getDeclaredMethods();
         
        System.out.println("Declared methods present in " +
                                            myClass.getName() + " class are : ");
         
        // iterating through declared Methods of Test class
        for (Method Method : m)
        {
            System.out.println(Method);
        }
    }
}

输出:

Declared methods present in Test class are : 
public static void Test.main(java.lang.String[])
public void Test.m1()
static void Test.m2()

11. Constructor getDeclaredConstructor(Class…parameterTypes) :该方法返回一个Constructor对象,该对象反映了该Class对象所代表的类或接口的指定构造函数。

Syntax : 
public Constructor getDeclaredConstructor(Class... parameterTypes) 
throws NoSuchMethodException,SecurityException
Parameters : 
parameterTypes - the list of parameters
Returns :
The Constructor object for the constructor with the specified parameter list
Throws :
NoSuchMethodException - if a Constructor with the specified parameterTypes is not found.
SecurityException - If a security manager, s, is present.

Java

// Java program to demonstrate getDeclaredConstructor() Constructor
 
import java.lang.reflect.Constructor;
 
public class Test
{
     
    public static void main(String[] args)
            throws NoSuchMethodException, SecurityException, ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.Integer");
        Class c2 = Class.forName("java.lang.String");
         
        // getDeclaredConstructor on myClass
        Constructor con = c1.getDeclaredConstructor(c2);
         
        System.out.println("Declared Constructor present in " + c1.getName() +
                                  " class specified by argument : " + c2.getName());
         
        System.out.println(con);
    }
}

输出:

Declared Constructor present in java.lang.Integer class specified by argument : 
java.lang.String public java.lang.Integer(java.lang.String) 
throws java.lang.NumberFormatException

12. Constructor[] getDeclaredConstructors() :该方法返回一个 Constructor 对象数组,反映了该 Class 对象所代表的类所声明的所有构造函数。这些是公共的、受保护的、默认(包)访问和私有构造函数。
如果此 Class 对象表示接口、原始类型、数组类或 void,则此方法返回长度为 0 的数组。

Syntax : 
public Constructor[] getDeclaredConstructors() throws SecurityException
Parameters : 
NA
Returns :
the array of Constructor objects representing all the declared constructors of this class
Throws :
SecurityException - If a security manager, s, is present.

Java

// Java program to demonstrate getDeclaredConstructors() Constructor
 
import java.lang.reflect.Constructor;
 
public class Test
{
     
    public static void main(String[] args)
            throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c = Class.forName("java.lang.String");
         
        // getDeclaredConstructors on myClass
        Constructor con[] = c.getDeclaredConstructors();
         
        System.out.println("Declared Constructors present in " +
                                c.getName() + " class are : ");
         
        // iterating through all constructors
        for (Constructor constructor : con)
        {
            System.out.println(constructor);
        }
    }
}

输出:

Declared Constructors present in java.lang.String class are : 
public java.lang.String(byte[],int,int)
public java.lang.String(byte[],java.nio.charset.Charset)
public java.lang.String(byte[],java.lang.String) throws 
java.io.UnsupportedEncodingException
public java.lang.String(byte[],int,int,java.nio.charset.Charset)
public java.lang.String(byte[],int,int,java.lang.String) 
throws java.io.UnsupportedEncodingException
java.lang.String(char[],boolean)
public java.lang.String(java.lang.StringBuilder)
public java.lang.String(java.lang.StringBuffer)
public java.lang.String(byte[])
public java.lang.String(int[],int,int)
public java.lang.String()
public java.lang.String(char[])
public java.lang.String(java.lang.String)
public java.lang.String(char[],int,int)
public java.lang.String(byte[],int)
public java.lang.String(byte[],int,int,int)

13. Class getDeclaringClass() :如果这个Class对象代表的类或接口是另一个类的成员,那么这个方法返回代表它被声明的类的Class对象。
如果此类或接口不是任何其他类的成员,则此方法返回 null。如果此 Class 对象表示数组类、原始类型或 void,则此方法返回 null。

Syntax : 
public Class  getDeclaringClass()
Parameters : 
NA
Returns :
the declaring class of this class

Java

// Java program to demonstrate
// getDeclaringClass() Class
import java.lang.reflect.Method;
public class Test
{
    // any method
    public void m1()
    {
        System.out.println("Inside m1 method");
    }
     
    public static void main(String[] args)
    {
         
        // returns A class object
        Class c1 = Test.class;
         
        // getting all methods of Test class
        // Note that methods from Object class
        // are also inherited
        Method m[] = c1.getMethods();
         
        for (Method method : m)
        {
            // getDeclaringClass method
            // it return declared class of this method
            System.out.println(method.getName() + " is present in "
                                            + method.getDeclaringClass());
        }
         
    }
}

输出:

main is present in class Test
m1 is present in class Test
wait is present in class java.lang.Object
wait is present in class java.lang.Object
wait is present in class java.lang.Object
equals is present in class java.lang.Object
toString is present in class java.lang.Object
hashCode is present in class java.lang.Object
getClass is present in class java.lang.Object
notify is present in class java.lang.Object
notifyAll is present in class java.lang.Object

14. Class getEnclosureClass() :此方法返回底层类的直接封闭类。如果底层类是顶级类,则此方法返回 null。

Syntax : 
public Class getEnclosingClass()
Parameters : 
NA
Returns :
the immediately enclosing class of the underlying class

Java

// Java program to demonstrate getEnclosingClass() Class
 
public class Test
{
    // any inner class of Test class
    class A{}
     
    public static void main(String[] args)
    {
         
        // returns A class object
        Class c1 = Test.A.class;
         
        // getEnclosingClass method
        // it returns the class object where A class present
        Class c2 = c1.getEnclosingClass();
         
        System.out.println("The class " + c1.getName() + " is present in class : ");
         
        System.out.println(c2);
         
    }
}

输出:

The class Test$A is present in class : 
class Test

15. Method getEnclosureMethod() :如果这个Class对象代表一个方法中的一个本地或匿名类,返回一个代表底层类的直接封闭方法的Method对象。

Syntax : 
public Class getEnclosingMethod()
Parameters : 
NA
Returns :
the immediately enclosing method of the underlying class,
if that class is a local or anonymous class;
otherwise null

Java

// Java program to demonstrate getEnclosingMethod() method
 
import java.lang.reflect.Method;
 
public class Test
{
    // any method
    public static Class m1()
    {  
        // any local class in m1
        class A{};
         
        // returning class A
        return A.class;
    }
 
     
    // main method
    public static void main(String[] args)
            throws ClassNotFoundException
    {
        // returns A Class object
        Class c = Test.m1();
         
        // getEnclosingMethod method
        Method m = c.getEnclosingMethod();
         
        System.out.println("The class " + c.getName() + " is present in method : ");
         
        System.out.println(m);
         
    }
}

输出:

The class Test$1A is present in method : 
public static java.lang.Class Test.m1()

16. Constructor getEnclosureConstructor() :如果这个Class对象代表一个构造函数中的本地或匿名类,返回一个Constructor对象,代表底层类的直接封闭构造函数。否则返回 null。

Syntax : 
public Constructor getEnclosingConstructor()
Parameters : 
NA
Returns :
the immediately enclosing constructor of the underlying class,
if that class is a local or anonymous class; 
otherwise null.

Java

// Java program to demonstrate
// getEnclosingConstructor() Constructor
 
import java.lang.reflect.Constructor;
 
public class Test
{
    Class c;
     
    // default(any) constructor
    public Test()
    {
        // any local class
        class A{};
         
        // returns A class object
        c = A.class;
    }
     
    public static void main(String[] args)
    {
        // creating Test class object
        Test t = new Test();
         
        // getEnclosingConstructor method
        Constructor con = t.c.getEnclosingConstructor();
         
        System.out.println("The class " + t.c.getName()
                        + " is present in constructor : ");
         
        System.out.println(con);
         
    }
}

输出:

The class Test$1A is present in Constructor : 
public Test()

17. ProtectionDomain getProtectionDomain() :返回该类的ProtectionDomain。如果安装了安全管理器,该方法首先调用安全管理器的 checkPermission 方法,并带有 RuntimePermission(“getProtectionDomain”) 权限,以确保可以获取 ProtectionDomain。

Syntax : 
public ProtectionDomain getProtectionDomain()
Parameters : 
NA
Returns :
the ProtectionDomain of this class
Throws :
SecurityException - if a security manager s exists 
and its checkPermission method doesn't allow getting the ProtectionDomain.

Java

// Java program to demonstrate getProtectionDomain() method
public class Test
{
    public static void main(String[] args)
                         throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.String");
         
        // checking for assertion status of String class
         
        System.out.println("protection domain of " + c1.getName() + " class : ");
         
        // getProtectionDomain() method
        // it will print null as String class is loaded by
        // BootStrap class loader
        System.out.println(c1.getProtectionDomain());
         
    }
}

输出:

protection domain of java.lang.String class : 
ProtectionDomain  null
 null
 
 java.security.Permissions@7852e922 (
 ("java.security.AllPermission" "" "")
)

18. boolean isAnnotationPresent(Class annotationClass)() :如果此元素上存在指定类型的注释,则此方法返回 true,否则返回 false。此方法主要是为了方便访问标记注释而设计的。

Specified by:
isAnnotationPresent in interface AnnotatedElement
Syntax : 
public boolean isAnnotationPresent(Class annotationClass) 
Parameters : 
annotationClass - the Class object corresponding to the annotation type
Returns :
true if an annotation for the specified annotation type is present on this element
false, otherwise
Throws:
NullPointerException - if the given annotation class is null

19. boolean isSynthetic() :此方法确定此类是否为合成类。

Syntax : 
public boolean isSynthetic()
Parameters : 
NA
Returns :
return true if and only if this class is a synthetic class.

20. URL getResource(String name) :查找具有给定名称的资源。搜索与给定类关联的资源的规则由该类的定义类加载器实现。

Syntax : 
public URL getResource(String name)
Parameters : 
name - name of the desired resource
Returns :
A URL object or null if no resource with this name is found

21. InputStream getResourceAsStream(String name) :查找具有给定名称的资源。搜索与给定类关联的资源的规则由该类的定义类加载器实现。

Syntax : 
public InputStream getResourceAsStream(String name)
Parameters : 
name - name of the desired resource
Returns :
A InputStream object or null if no resource with this name is found
Throws:
NullPointerException - If name is null

22. Object[] getSigners() :获取该类的签名者。

Syntax : 
public Object[] getSigners()
Parameters : 
NA
Returns :
the signers of this class, or null if there are no signers.
In particular,it returns null if this object represents a primitive type or void.

23. Annotation[] getAnnotations() :此方法返回此元素上存在的所有注释。如果此元素没有注释,则返回一个长度为零的数组。
该方法的调用者可以随意修改返回的数组;它不会影响返回给其他调用者的数组。

Specified by:
getAnnotations() in interface AnnotatedElement
Syntax : 
public Annotation[] getAnnotations()
Parameters : 
NA
Returns :
all annotations present on this element

24. A getAnnotation(Class annotationClass) :如果存在这样的注释,则此方法返回此元素的指定类型的注释,否则返回 null。

25. Annotation[] getDeclaredAnnotations() :返回直接存在于该元素上的所有注释。与此接口中的其他方法不同,此方法忽略继承的注解。如果此元素上没有直接存在注释,则返回一个长度为零的数组。
该方法的调用者可以随意修改返回的数组;它不会影响返回给其他调用者的数组。