# Get the Name of a Member Object in Java

Java 8Object Oriented ProgrammingProgramming

The getName() method is used to get the names of the entities such as interface, class, array class, void etc. that are represented by the class objects. These names are returned in the form of a string.

A program that gets the name of the member objects using getName() method is given as follows −

## Example

Live Demo

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] argv) throws Exception {
Class c = java.lang.Integer.class;
Method m = c.getMethods()[0];
Field f = c.getFields()[0];
Constructor cons = c.getConstructors()[0];
String name;
name = c.getName();
System.out.println("Name of class: " + name);
name = m.getName();
System.out.println("Name of method: " + name);
name = f.getName();
System.out.println("Name of field: " + name);
name = cons.getName();
System.out.println("Name of constructor: " + name);
}
}

## Output

Name of class: java.lang.Integer
Name of field: MIN_VALUE
Name of constructor: java.lang.Integer

Now let us understand the above program.

The getName() method is used to get the names of the class, method, field and constructor. Then these are displayed. A code snippet which demonstrates this is as follows −

Class c = java.lang.Integer.class;
Method m = c.getMethods()[0];
Field f = c.getFields()[0];
Constructor cons = c.getConstructors()[0];
String name;
name = c.getName();
System.out.println("Name of class: " + name);
name = m.getName();
System.out.println("Name of method: " + name);
name = f.getName();
System.out.println("Name of field: " + name);
name = cons.getName();
System.out.println("Name of constructor: " + name);
Published on 09-Jan-2019 07:21:31