- Trending Categories
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
Find the Package of an Object in Java
The package for an object of a class can be obtained using the 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.ArrayList; import java.util.LinkedList; import java.util.HashSet; public class Main { public static void main(String[] args) { System.out.println("The package is: " + new ArrayList().getClass().getPackage().getName()); System.out.println("The package is: " + new LinkedList().getClass().getPackage().getName()); System.out.println("The package is: " + new HashSet().getClass().getPackage().getName()); System.out.println("The package is: " + "This is a string".getClass().getPackage().getName()); System.out.println("The package is: " + new Double(1).getClass().getPackage().getName()); System.out.println("The package is: " + new Integer(1).getClass().getPackage().getName()); } }
Output
The package is: java.util The package is: java.util The package is: java.util The package is: java.lang The package is: java.lang The package is: java.lang
Now let us understand the above program.
The package is obtained and printed by using getClass().getPackage().getName(). A code snippet which demonstrates this is as follows −
System.out.println("The package is: " + new ArrayList().getClass().getPackage().getName()); System.out.println("The package is: " + new LinkedList().getClass().getPackage().getName()); System.out.println("The package is: " + new HashSet().getClass().getPackage().getName()); System.out.println("The package is: " + "This is a string".getClass().getPackage().getName()); System.out.println("The package is: " + new Double(1).getClass().getPackage().getName()); System.out.println("The package is: " + new Integer(1).getClass().getPackage().getName());
- Related Articles
- How to find package explorer in Java eclipse project?
- Display the package name of a class in Java
- Check for the availability of a package in Java
- Package Imports in JShell of Java 9
- How to access Java package from another package
- Get the Component Type of an Array Object in Java
- overriding method different package in java
- Getting an error message Object already exported, no package change is possible while changing a package for 250 SAP development objects
- Get super class of an object in Java
- What is the package for String Class in Java?
- How to find the name of the author of a package in R?
- Create an Integer object in Java
- Can we create an object of an abstract class in Java?
- Accessing a Java class in other package.
- How to use sub-package in Java?

Advertisements