Home » Java programming language

Java Class class getDeclaringClass() method with example

Class class getDeclaringClass() method: Here, we are going to learn about the getDeclaringClass() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 04, 2019

Class class getDeclaringClass() method

  • getDeclaringClass() method is available in java.lang package.
  • getDeclaringClass() method is used to return the declared Class object denoting the Class in which it is declared when this class or interface denoted by this Class is a part or member of other class.
  • getDeclaringClass() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getDeclaringClass() method does not throw an exception at the time of returning Class object.

Syntax:

    public Class getDeclaringClass ();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Class, it returns an object of declared class from this Class.

Note:

  • When the class or interface is not part or member of other class, null is returned.
  • When this class reflects primitive or void type or an array class, null is returned.

Example:

// Java program to demonstrate the example 
// of Class getDeclaringClass() method of Class 

import java.lang.reflect.*;
public class GetDeclaringClassOfClass {
    public static void main(String[] args) {
        // It returns the Class object with 
        // the given class name
        Class cl = Integer.class;

        // It returns an array of Methods 
        // that denotes the private, protected, public and default 
        // Methods of the class Integer
        Method[] methods = cl.getMethods();

        // Traverse Integer class
        for (int i = 0; i < methods.length; ++i) {
            Class declare_classes = methods[i].getDeclaringClass();
            System.out.print(" Declaring Methods Class: ");
            System.out.println(declare_classes.toString());
        }
    }
}

Output

 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Integer
 Declaring Methods Class: class java.lang.Object
 Declaring Methods Class: class java.lang.Object
 Declaring Methods Class: class java.lang.Object
 Declaring Methods Class: class java.lang.Object
 Declaring Methods Class: class java.lang.Object
 Declaring Methods Class: class java.lang.Object


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.