

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the Name of a Member Object in Java
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
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 method: numberOfLeadingZeros 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);
- Related Questions & Answers
- Get the unqualified name of a class in Java
- Get the name of a primitive type in Java
- Get the fully-qualified name of a class in Java
- Get the length of a StringBuffer Object in Java
- Get the name of the file and path in Java
- Get the capacity of the StringBuffer Object in Java
- Member variables in Java
- Get Canonical Name for a class in Java
- Calling a member function on a NULL object pointer in C++
- How to access a derived class member variable by an interface object in Java?
- How can we get the name of the Enum constant in Java?
- Get the fully-qualified name of an inner class in Java
- Get localized day name in Java
- Get file extension name in Java
- Java Program to get name of parent directory
Advertisements