- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Get the length of a StringBuffer Object in Java
- 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
- How to access a derived class member variable by an interface object in Java?
- Get the capacity of the StringBuffer Object in Java
- Member variables in Java
- Get the name of the file and path in Java
- Calling a member function on a NULL object pointer in C++
- Get the Component Type of an Array Object in Java
- Get super class of an object in Java
- Get Canonical Name for a class in Java
- Get the String representation of the current File object in Java
- Give the name and structural formula of one member of the following: Alcohols
- Get localized day name in Java

Advertisements