

- 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 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 Questions & Answers
- 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
- Name the various annotations available in TestNG.
- What is the difference between simple name, canonical name and class name in a Java class?
- Using predefined class name as Class or Variable name in Java
- Why the constructor name is same as the class name in Java?
- Get day name for the corresponding date in MySQL?
- Create Objects of a Class in Java
- Display the package name of a class in Java
- How to get the class name of an instance in Python?
- How to get css class name using Selenium?
- Get localized day name in Java
- Get file extension name in Java
Advertisements