Java Package - Javatpoint
Java Package - Javatpoint
p y
Control Statements
J C t l St t t
next → ← prev
Java Package
A java package is a group of similar types of
1. Java Package
classes, interfaces and sub-packages.
2. Example of package
Package in java can be categorized in two form, 3. Accessing package
built-in package and user-defined package. 1. By import
packagename.*
There are many built-in packages such as java, 2. By import
lang, awt, javax, swing, net, io, util, sql etc. packagename.classname
3. By fully qualified name
Here, we will have the detailed learning of creating
4. Subpackage
and using user-defined packages.
5. Sending class file to
another directory
6. -classpath switch
7. 4 ways to load the class file
or jar file
8. How to put two public
class in a package
9. Static Import
10. Package class
https://www.javatpoint.com/package 2/20
1/8/25, 11:41 PM Java Package - javatpoint
//save as Simple.java
package mypack;
public class Simple{
https://www.javatpoint.com/package 3/20
1/8/25, 11:41 PM Java Package - javatpoint
For example
javac -d . Simple.java
The -d switch specifies the destination where to put the generated class file. You can use
any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you
want to keep the package within the same directory, you can use . (dot).
Output:Welcome to package
https://www.javatpoint.com/package 4/20
1/8/25, 11:41 PM Java Package - javatpoint
The -d is a switch that tells the compiler where to put the class file i.e. it represents
destination. The . represents the current folder.
1. import package.*;
2. import package.classname;
3. fully qualified name.
1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible
but not subpackages.
The import keyword is used to make the classes and interface of another package
accessible to the current package.
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
https://www.javatpoint.com/package 5/20
1/8/25, 11:41 PM Java Package - javatpoint
2) Using packagename.classname
If you import package.classname then only declared class of this package will be
accessible.
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
https://www.javatpoint.com/package 6/20
1/8/25, 11:41 PM Java Package - javatpoint
India to Dubai
from India
from Rs14,953 Search
India to Malaysia
from India
from Rs5,517 Search
It is generally used when two packages have same class name e.g. java.util and java.sql
India to Thailand
packages contain Date class.
from India
from Rs10,668 Search
*
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
Output:Hello
If you import a package, all the classes and interface of that package will be imported
excluding the classes and interfaces of the subpackages. Hence, you need to import the
subpackage as well.
https://www.javatpoint.com/package 7/20
1/8/25, 11:41 PM Java Package - javatpoint
Subpackage in java
Package inside the package is called the subpackage. It should be created to
categorize the package further.
Advertisement
https://www.javatpoint.com/package 8/20
1/8/25, 11:41 PM Java Package - javatpoint
Let's take an example, Sun Microsystem has definded a package named java that
contains many classes like System, String, Reader, Writer, Socket etc. These classes
represent a particular group e.g. Reader and Writer classes are for Input/Output
India to Dubai
operation, Socket and ServerSocket classes are for networking etc and so on. So, Sun has
subcategorized the java package into subpackages such as lang, net, io etc. and put the
Input/Output related classes in io package, Server and ServerSocket classes in net
packages and so on. from India
Example of Subpackage
package com.javatpoint.core;
class Simple{
public static void main(String args[]){
System.out.println("Hello subpackage");
}
}
Output:Hello subpackage
https://www.javatpoint.com/package 9/20
1/8/25, 11:41 PM Java Package - javatpoint
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
To Compile:
To Run:
https://www.javatpoint.com/package 10/20
1/8/25, 11:41 PM Java Package - javatpoint
To run this program from e:\source directory, you need to set classpath of the
directory where the class file resides.
e:\sources> set classpath=c:\classes;.;
e:\sources> java mypack.Simple
To run this program from e:\source directory, you can use -classpath switch of java that
tells where to look for class file. For example:
Output:Welcome to package
Rule: There can be only one public class in a java source file and it must be
saved by the public class name.
class A{}
class B{}
public class C{}
https://www.javatpoint.com/package 11/20
1/8/25, 11:41 PM Java Package - javatpoint
//save as A.java
package javatpoint;
public class A{}
//save as B.java
package javatpoint;
public class B{}
2. What is the correct way to import all classes from a package named myPackage?
1. import myPackage;
2. import myPackage.*;
https://www.javatpoint.com/package 12/20