- 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 class name for various objects 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. The getPackage() method gets the package for the given class.
A program that gets the class name for various objects is given as follows −
Example
package Test; import java.io.IOException; import java.util.HashMap; public class Demo { public static void main(String args[]) throws IOException { Object obj = "string"; System.out.println("The class name is: " + obj.getClass().getName()); obj = new HashMap(); System.out.println("The class name is: " + obj.getClass().getName()); Boolean bool = new Boolean(false); obj = bool; System.out.println("The class name is: " + obj.getClass().getName()); System.out.println("The package name is: " + Demo.class.getPackage()); } }
Output
The class name is: java.lang.String The class name is: java.util.HashMap The class name is: java.lang.Boolean The package name is: package Test
Now let us understand the above program.
The getName() method is used to get the names of various objects such as a String object, HashMap object, Boolean object etc. Then the getPackage() method is used to get the package for the given class which is Test.
- Related Articles
- Get Canonical Name for a class in Java
- Get the unqualified name of a class in Java
- Get the fully-qualified name of a class in Java
- Get the fully-qualified name of an inner class in Java
- Create Objects of a Class in Java
- Using predefined class name as Class or Variable name in Java
- What is the difference between simple name, canonical name and class name in a Java class?
- Create class Objects in Java using new operator
- Why the constructor name is same as the class name in Java?
- The get() method of AbstractList class in Java
- How to get the class name of an instance in Python?
- Name the various annotations available in TestNG.
- Display the package name of a class in Java
- The get() method of Java AbstractSequentialList class
- How to get objects by ID, Class, Tag, and Attribute in jQuery?

Advertisements