How to work with getDeclaringClass() in Java

Java 8Object Oriented ProgrammingProgramming

The getDeclaringClass() method returns the Class object for the class in which the object was declared. This happens only if the Class of the Class object is a member of another class. Otherwise this method returns null.

Also, if a primitive type, array class, void etc. are represented by the Class object, then the getDeclaringClass() method returns null.

A program that demonstrates the getDeclaringClass() method is given as follows −

Example

 Live Demo

package Test;
import java.lang.reflect.*;
public class Demo {
   public static void main(String[] args) {
      Method[] m = String.class.getMethods();
      for(int i = 0; i < m.length; i++) {
         Class decClass = m[i].getDeclaringClass();
         System.out.println("\nThe Method name is: " + m[i].toString());
         System.out.println("The Declaring class name is: " + decClass.toString());
      }
   }
}

Output

The Method name is: public boolean java.lang.String.equals(java.lang.Object)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.toString()
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.hashCode()
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.compareTo(java.lang.Object)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.compareTo(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.indexOf(java.lang.String,int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.indexOf(int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.indexOf(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.indexOf(int,int)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.valueOf(char)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.valueOf(java.lang.Object)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.valueOf(boolean)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.valueOf(char[],int,int)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.valueOf(char[])
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.valueOf(double)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.valueOf(float)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.valueOf(long)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.valueOf(int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.length()
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.isEmpty()
The Declaring class name is: class java.lang.String

The Method name is: public char java.lang.String.charAt(int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.codePointAt(int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.codePointBefore(int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.codePointCount(int,int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.offsetByCodePoints(int,int)
The Declaring class name is: class java.lang.String

The Method name is: public void java.lang.String.getChars(int,int,char[],int)
The Declaring class name is: class java.lang.String

The Method name is: public byte[] java.lang.String.getBytes()
The Declaring class name is: class java.lang.String

The Method name is: public byte[] java.lang.String.getBytes(java.lang.String) throws java.io.UnsupportedEncodingException
The Declaring class name is: class java.lang.String

The Method name is: public void java.lang.String.getBytes(int,int,byte[],int)
The Declaring class name is: class java.lang.String

The Method name is: public byte[] java.lang.String.getBytes(java.nio.charset.Charset)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.contentEquals(java.lang.StringBuffer)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.contentEquals(java.lang.CharSequence)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.equalsIgnoreCase(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.compareToIgnoreCase(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.regionMatches(int,java.lang.String,int,int)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.regionMatches(boolean,int,java.lang.String,int,int)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.startsWith(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.startsWith(java.lang.String,int)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.endsWith(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.lastIndexOf(int,int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.lastIndexOf(java.lang.String,int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.lastIndexOf(int)
The Declaring class name is: class java.lang.String

The Method name is: public int java.lang.String.lastIndexOf(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.substring(int)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.substring(int,int)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.CharSequence java.lang.String.subSequence(int,int)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.concat(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.replace(char,char)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.matches(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public boolean java.lang.String.contains(java.lang.CharSequence)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.replaceFirst(java.lang.String,java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.replaceAll(java.lang.String,java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String[] java.lang.String.split(java.lang.String,int)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String[] java.lang.String.split(java.lang.String)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.CharSequence[])
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.Iterable)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.toLowerCase(java.util.Locale)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.toLowerCase()
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.toUpperCase()
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.toUpperCase(java.util.Locale)
The Declaring class name is: class java.lang.String

The Method name is: public java.lang.String java.lang.String.trim()
The Declaring class name is: class java.lang.String

The Method name is: public char[] java.lang.String.toCharArray()
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.format(java.util.Locale,java.lang.String,java.lang.Object[])
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.format(java.lang.String,java.lang.Object[])
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.copyValueOf(char[],int,int)
The Declaring class name is: class java.lang.String

The Method name is: public static java.lang.String java.lang.String.copyValueOf(char[])
The Declaring class name is: class java.lang.String

The Method name is: public native java.lang.String java.lang.String.intern()
The Declaring class name is: class java.lang.String

The Method name is: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
The Declaring class name is: class java.lang.Object

The Method name is: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
The Declaring class name is: class java.lang.Object

The Method name is: public final void java.lang.Object.wait() throws java.lang.InterruptedException
The Declaring class name is: class java.lang.Object

The Method name is: public final native java.lang.Class java.lang.Object.getClass()
The Declaring class name is: class java.lang.Object

The Method name is: public final native void java.lang.Object.notify()
The Declaring class name is: class java.lang.Object

The Method name is: public final native void java.lang.Object.notifyAll()
The Declaring class name is: class java.lang.Object

The Method name is: public default java.util.stream.IntStream java.lang.CharSequence.codePoints()
The Declaring class name is: interface java.lang.CharSequence

The Method name is: public default java.util.stream.IntStream java.lang.CharSequence.chars()
The Declaring class name is: interface java.lang.CharSequence
raja
Updated on 25-Jun-2020 12:54:34

Advertisements