Get class members information using reflection

We can get the class members information using reflection. For example, we can know what are the fields, constructors and methods have been defined inside the class. Let’s see an example where we want to know what are the fields, constructors and methods in String class.

package reflection1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Example2 {
	public static void main(String[] args) throws ClassNotFoundException {
		
		// get Class object
		Class classObj = Class.forName("java.lang.String");

		// get all field defined in Example5 class
		System.out.println("-----get filed name defined in String class----");
		Field[] fieldArr = classObj.getDeclaredFields();
		for (Field field : fieldArr) {
			System.out.println(field.getName());
		}
		
		System.out.println("-----get constructors defined in String class----");
		Constructor[] constArr = classObj.getDeclaredConstructors();
		for (Constructor constr : constArr) {
			System.out.println(constr.toGenericString());
		}
		
		

		System.out.println("-----get all defined methods of defined in String class----");
		Method[] methodArr = classObj.getDeclaredMethods();
		for (Method method : methodArr) {
			System.out.println(method.toGenericString());
		}
		
		
	}
}

 

Output is –

—–get filed name defined in String class—-
value
hash
serialVersionUID
serialPersistentFields
CASE_INSENSITIVE_ORDER
—–get constructors defined in String class—-
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)
—–get all defined methods of defined in String class—-
public boolean java.lang.String.equals(java.lang.Object)
public java.lang.String java.lang.String.toString()
public int java.lang.String.hashCode()
public int java.lang.String.compareTo(java.lang.String)
public int java.lang.String.compareTo(java.lang.Object)
public int java.lang.String.indexOf(java.lang.String,int)
public int java.lang.String.indexOf(java.lang.String)
public int java.lang.String.indexOf(int,int)
public int java.lang.String.indexOf(int)
static int java.lang.String.indexOf(char[],int,int,char[],int,int,int)
static int java.lang.String.indexOf(char[],int,int,java.lang.String,int)
public static java.lang.String java.lang.String.valueOf(int)
public static java.lang.String java.lang.String.valueOf(long)
public static java.lang.String java.lang.String.valueOf(float)
public static java.lang.String java.lang.String.valueOf(boolean)
public static java.lang.String java.lang.String.valueOf(char[])
public static java.lang.String java.lang.String.valueOf(char[],int,int)
public static java.lang.String java.lang.String.valueOf(java.lang.Object)
public static java.lang.String java.lang.String.valueOf(char)
public static java.lang.String java.lang.String.valueOf(double)
public char java.lang.String.charAt(int)
private static void java.lang.String.checkBounds(byte[],int,int)
public int java.lang.String.codePointAt(int)
public int java.lang.String.codePointBefore(int)
public int java.lang.String.codePointCount(int,int)
public int java.lang.String.compareToIgnoreCase(java.lang.String)
public java.lang.String java.lang.String.concat(java.lang.String)
public boolean java.lang.String.contains(java.lang.CharSequence)
public boolean java.lang.String.contentEquals(java.lang.CharSequence)
public boolean java.lang.String.contentEquals(java.lang.StringBuffer)
public static java.lang.String java.lang.String.copyValueOf(char[])
public static java.lang.String java.lang.String.copyValueOf(char[],int,int)
public boolean java.lang.String.endsWith(java.lang.String)
public boolean java.lang.String.equalsIgnoreCase(java.lang.String)
public static java.lang.String java.lang.String.format(java.util.Locale,java.lang.String,java.lang.Object…)
public static java.lang.String java.lang.String.format(java.lang.String,java.lang.Object…)
public void java.lang.String.getBytes(int,int,byte[],int)
public byte[] java.lang.String.getBytes(java.nio.charset.Charset)
public byte[] java.lang.String.getBytes(java.lang.String) throws java.io.UnsupportedEncodingException
public byte[] java.lang.String.getBytes()
public void java.lang.String.getChars(int,int,char[],int)
void java.lang.String.getChars(char[],int)
private int java.lang.String.indexOfSupplementary(int,int)
public native java.lang.String java.lang.String.intern()
public boolean java.lang.String.isEmpty()
public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.CharSequence…)
public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.Iterable<? extends java.lang.CharSequence>)
public int java.lang.String.lastIndexOf(int)
public int java.lang.String.lastIndexOf(java.lang.String)
static int java.lang.String.lastIndexOf(char[],int,int,java.lang.String,int)
public int java.lang.String.lastIndexOf(java.lang.String,int)
public int java.lang.String.lastIndexOf(int,int)
static int java.lang.String.lastIndexOf(char[],int,int,char[],int,int,int)
private int java.lang.String.lastIndexOfSupplementary(int,int)
public int java.lang.String.length()
public boolean java.lang.String.matches(java.lang.String)
private boolean java.lang.String.nonSyncContentEquals(java.lang.AbstractStringBuilder)
public int java.lang.String.offsetByCodePoints(int,int)
public boolean java.lang.String.regionMatches(int,java.lang.String,int,int)
public boolean java.lang.String.regionMatches(boolean,int,java.lang.String,int,int)
public java.lang.String java.lang.String.replace(char,char)
public java.lang.String java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence)
public java.lang.String java.lang.String.replaceAll(java.lang.String,java.lang.String)
public java.lang.String java.lang.String.replaceFirst(java.lang.String,java.lang.String)
public java.lang.String[] java.lang.String.split(java.lang.String)
public java.lang.String[] java.lang.String.split(java.lang.String,int)
public boolean java.lang.String.startsWith(java.lang.String,int)
public boolean java.lang.String.startsWith(java.lang.String)
public java.lang.CharSequence java.lang.String.subSequence(int,int)
public java.lang.String java.lang.String.substring(int)
public java.lang.String java.lang.String.substring(int,int)
public char[] java.lang.String.toCharArray()
public java.lang.String java.lang.String.toLowerCase(java.util.Locale)
public java.lang.String java.lang.String.toLowerCase()
public java.lang.String java.lang.String.toUpperCase()
public java.lang.String java.lang.String.toUpperCase(java.util.Locale)
public java.lang.String java.lang.String.trim()

 

Let’s see another example where we will have a user-defined class with field, constructor, and methods.

package reflection1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Example3 {
	private String name;
	String id;

	public Example3(int a) {

	}

	private Example3() {

	}

	private void m1() {

	}

	public void m2() {

	}

	public static void main(String[] args) throws ClassNotFoundException {
		// get Class object
		Class classObj = Class.forName("reflection1.Example3");

		// get all field defined in Example5 class
		System.out.println("-----get filed name defined in Example3 class----");
		Field[] fieldArr = classObj.getDeclaredFields();
		for (Field field : fieldArr) {
			System.out.println(field.getName());
		}

		System.out.println("-----get constructors defined in Example3 class----");
		Constructor[] constArr = classObj.getDeclaredConstructors();
		for (Constructor constr : constArr) {
			System.out.println(constr.toGenericString());
		}

		System.out.println("-----get all defined methods of defined in Example3 class----");
		Method[] methodArr = classObj.getDeclaredMethods();
		for (Method method : methodArr) {
			System.out.println(method.toGenericString());
		}

	}
}

Output is –

—–get filed name defined in Example3 class—-
name
id
—–get constructors defined in Example3 class—-
public reflection1.Example3(int)
private reflection1.Example3()
—–get all defined methods of defined in Example3 class—-
public static void reflection1.Example3.main(java.lang.String[]) throws java.lang.ClassNotFoundException
private void reflection1.Example3.m1()
public void reflection1.Example3.m2()