V SURESH KUMAR AP/SVCAS
JAVA API PACKAGES
A package in Java is used to group related classes.
Think of it as a folder in a file directory.
Packages are divided into two categories:
1. Built-in Packages (packages from the Java API)
2. User-defined Packages (create your own packages)
Built-in Packages
The Java API is a library of prewritten classes, that are free to use and included in the Java
Development Environment.
The library contains components for managing input, database programming, and much
more.
The library is divided into packages and classes.
Java API packages or built-in packages
Java provides a large number of classes grouped into different packages based on a
particular functionality.
Examples
java.lang:
It contains classes for primitive types, strings, math functions, threads, and exceptions.
java.util:
It contains classes such as vectors, hash tables, dates, Calendars, etc.
java.io:
It has stream classes for Input/Output.
java.awt:
Classes for implementing Graphical User Interface – windows, buttons, menus, etc.
java.net:
Classes for networking
java. Applet:
Classes for creating and implementing applets
User-defined Packages
To create your own package, you need to understand that Java uses a file system directory to
store them. Just like folders on your computer:
Example
└── root
└── my pack
└── MyPackageClass.java
To create a package, use the package keyword:
MyPackageClass.java
package mypack;
class MyPackageClass {
public static void main(String[] args) {
System.out.println("This is my package!");
}
}
Save the file as MyPackageClass.java, and compile it:
V SURESH KUMAR AP/SVCAS
C:\Users\...>javac MyPackageClass.java
Then compile the package:
C:\Users\...>javac -d . MyPackageClass.java
This forces the compiler to create the "mypack" package.
The -d keyword specifies the destination for where to save the class file.
To run the MyPackageClass.java file, write the following:
C:\Users\...>java mypack.MyPackageClass
The output will be:
This is my package!
NAMING CONVENTION OF THE PACKAGE
• Packages can be named using the Standard Java naming rules
• By convention, the package must begin with lowercase letters
• It is very easy to find related types because they contain types that are logically
related.
• We can uniquely identify the class name and avoid naming conflicts.
• We can easily control the access of types by combining access modifiers and
packages.
Example:
int x=2;
double y = java.lang.math.sqrt(x)
lang→ Package name
math→ class name
sqrt(x)→method name
• Every package name must be unique
• Duplicate names will cause run-time errors
Creating a package
We use the package keyword as the very first line of code in the file for creating a package
KEYWORD = package
This must be the first statement in the Java Source file
The syntax of creating a package in Java is as follows:
package package name;
Example
package bca;
public class aids
{
……………
body if the class;
V SURESH KUMAR AP/SVCAS
…………..
}
Explanation:
• Here the package name is the bca.
• The class aids the part of this package bca.
• The program will be saved as aids.java
• It is located in the directory bca
• When we compile the source file, Java will create the .class file
• .class file must be located in the same directory
To create a package, we must involve the following steps
1. Declare the package at the beginning of the file.
2. The class must be in a public
3. The method must be in public
4. To compile the file, the .class file must be created in the same directory
Java also supports the package hierarchy.
Eg:
package package1.package2
Accessing and Using a package
• Java system packages (i.e.) pre-defined packages can be accessed by using the
import statement in java
• The same approach can be used to access the user-defined packages.
• The import statement can be used to search the list of packages in a particular
class
The general form of package import statement is
import package1.package2. package3. Class name;
• Here package1 is the top-level package and others are the sub package of the top-
level package package1.
• The statement must end with a semicolon;
Using a package
• There are three ways to access the package from outside the package.
❖ import package.*;
❖ import package.classname;
❖ fully qualified name.
import package.*;
If you use package. * then all the classes and interfaces of this package will be
accessible
The import keyword is used to make the classes and interface of another package
accessible to the current package.
Example of package that imports the package name.*
V SURESH KUMAR AP/SVCAS
//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
Using packagename.classname
If you import a package. class name then only the declared class of this package will be
accessible.
Example of package by import package .class name
//save by A.java
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
V SURESH KUMAR AP/SVCAS
//save by B.java
import pack.A;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Output: Hello
fully qualified name
Using the fully qualified name
• If you use a fully qualified name then only the declared class of this package will
be accessible.
• Now there is no need to import. But you need to use a fully qualified name every
time when you access the class or interface.
It is generally used when two packages have the same class name
e.g. java.util and java.sql packages contain Date class.
Example of the package by import fully qualified name
//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