- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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);
Advertisements