Java Packages & Apis
Java Packages & Apis
OBJECT ORIENTED
PROGRAMMING I RCS 122
05/22/2025 7
Types of packages in Java
Built-in Packages: These packages are part of the Java
API and provide pre-written classes and interfaces for
common tasks.
Some commonly used built-in packages include:
java.lang: Contains fundamental classes like String,
Math, and Thread. It is imported by default.
java.io: Provides classes for input and output operations.
java.util: Includes utility classes for data structures like
ArrayList and HashMap, and date/time operations.
java.net: Contains classes for networking operations.
java.sql: Provides classes for accessing and processing
data from databases.
java.awt: Contains classes for creating graphical user 8
05/22/2025
Types of packages in Java cont..
User-defined Packages
These packages are created by developers to
organize their own classes and interfaces. They
help in modularizing code and improving
maintainability.
For example, a user might create a package
named com.example.myproject to store classes
related to their project.
05/22/2025 9
Creating and using Packages
To create a package, you choose a name for the package
(consider naming conventions) and put a package statement
with that name at the top of every source file that contains
the types (classes, interfaces, enumerations, and annotation
types) that you want to include in the package.
The package statement (for example, package graphics;)
must be the first line in the source file.
There can be only one package statement in each source file,
and it applies to all types in the file.
If you do not use a package statement, your type ends up in
an unnamed package. Generally speaking, an unnamed
package is only for small or temporary applications or when
you are just beginning the development process. Otherwise,
05/22/2025 10
Creating and using Packages cont..
Note:
- If you put multiple types in a single source file, only
one can be public, and it must have the same name
as the source file. For example, you can define
public class Circle in the file Circle.java, define
public interface Draggable in the file
Draggable.java, define public enum Day in the file
Day.java, and so forth.
- You can include non-public types in the same file as a
public type (this is strongly discouraged, unless the
non-public types are small and closely related to the
05/22/2025 11
public type), but only the public type will be
Creating and using Packages cont..
05/22/2025 14
Using package members
The types that comprise a package are known
as the package members.
To use a public package member from outside
its package, you must do one of the following:
a)Refer to the member by its fully qualified
name
b)Import the package member
c)Import the member's entire package
Each is appropriate for different situations, as
explained in the slides that follow.
05/22/2025 15
a) Referring to a package member by its Fully qualifi ed
name
So far, most of the examples in this tutorial have referred to
types by their simple names, such as “MyClass”. You can use a
package member's simple name if the code you are writing is
in the same package as that member or if that member has
been imported.
However, if you are trying to use a member from a different
package and that package has not been imported, you must
use the member's fully qualified name, which includes the
package name. Here is the fully qualified name for the
Rectangle class declared in the graphics package:
graphics.Rectangle
You could use this qualified name to create an instance of
graphics.Rectangle,
05/22/2025 like: 16
a) Referring to a package member by its Fully qualifi ed
name
05/22/2025 18
b) Importing a package member
To import a specific member into the current file, put an
import statement at the beginning of the file before any
type definitions but after the package statement, if there
is one.
Here's how you would import the Rectangle class from
the graphics package:
import graphics.Rectangle;
Now you can refer to the Rectangle class by its simple
name:
Rectangle myRect = new Rectangle();
This approach works well if you use just a few members
from the graphics package. But if you use many types
05/22/2025 19
b) Importing a package member
//save by B.java
//save by A.java
package mypack;
package pack;
public class A { import pack.A;
public void msg() { class B {
public static void
System.out.println("Hell main(String args[]) {
o"); A obj = new A();
} obj.msg();
} }
}
05/22/2025 20
c) Importing the entire package
05/22/2025 26
NEXT ON LECTURE
FOURTEEN
JAVA INTERFACE
05/22/2025 27
THANK YOU!
05/22/2025 28