- 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
Display the package name of a class in Java
The package for a class can be obtained using the java.lang.Class.getPackage() method with the help of the class loader of the class. If there is no package object created by the class loader of the class, then null is returned.
A program that demonstrates this is given as follows −
Example
import java.util.Date; public class Main { public static void main(String[] args) { Date d = new Date(); Package p = d.getClass().getPackage(); String pName = p.getName(); System.out.println("The package name is: " + pName); } }
Output
The package name is: java.util
Now let us understand the above program.
The getPackage() method is used to obtain the package for the class. Then getName() method is used to get the name of the package. Then this name is displayed. A code snippet which demonstrates this is as follows −
Date d = new Date(); Package p = d.getClass().getPackage(); String pName = p.getName(); System.out.println("The package name is: " + pName);
- Related Articles
- Accessing a Java class in other package.
- What is the package for String Class in Java?
- Get the unqualified name of a class in Java
- What would getPackage() return for a class in unnamed package in Java?
- What is the difference between simple name, canonical name and class name in a Java class?
- Get the fully-qualified name of a class in Java
- Can I define more than one public class in a Java package?
- Display the current method name in Java
- Display the Operating System name in Java
- Display File class constants in Java
- How to find the name of the author of a package in R?
- Check for the availability of a package in Java
- Using predefined class name as Class or Variable name in Java
- Get Canonical Name for a class in Java
- How to access the object of a class without using the class name from a static context in java?

Advertisements