
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can I import same package twice? Will JVM load the package twice at runtime?
In Java classes and interfaces related to each other are grouped under a package. Package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in java.io package.
Creating a package
You can group required classes and interfaces under one package just by declaring the package at the top of the Class/Interface (file) using the keyword package as −
Example
package com.tutorialspoint.mypackage; public class Sample{ public void demo(){ System.out.println("This is a method of the sample class"); } public static void main(String args[]){ System.out.println("Hello how are you......"); } }
Unlike other programs to compile a program with a package, you need to use the –d option of the javac command specifying the destination path where you need to create the package.
Output
javac –d . Sample.java
If you haven’t mentioned the destination path the package will be created in the current directory.
Importing a class
To access the classes/interfaces that are grouped under a package, you need to add the location of the package in the classpath variable (or make sure the package is in the current directory) and import the class/interface of it using the import keyword.
Example
import com.tutorialspoint.mypackage.Sample; public class Test{ public static void main(String args[]){ Sample obj = new Sample(); obj.demo(); } }
Output
This is a method of the sample class
Importing a class twice
Yes, you can import a class twice in Java, it doesn’t create any issues but, irrespective of the number of times you import, JVM loads the class only once.
Example
In the following Java program, we are trying to import the Sample class of the com.tutorialspoint.mypackage package only once.
import com.tutorialspoint.mypackage.Sample; import com.tutorialspoint.mypackage.Sample; public class Test{ public static void main(String args[]){ Sample obj = new Sample(); obj.demo(); } }
Output
Sample class loaded This is a method of the sample class
- Related Articles
- How to load classes at runtime from a folder or Java package
- How to import Pandas package?
- Can we define a package after the import statement in Java?
- How do I import all the submodules of a Python namespace package?
- Difference between import and package in Java?
- Do I need to import the Java.lang package anytime during running a program?
- How to import everything from a python namespace / package?
- How to import classes from within another directory/package in Java?
- Is there a need to import Java.lang package while running Java programs?
- How to access Java package from another package
- How to install and import Python modules at runtime?
- Can I define more than one public class in a Java package?
- Why we do not import a package while we use any string function?
- How do I write package names in Java?
- How to resolve "Could not find or load main class package" in Java?
