PACKAGES
PACKAGES
A java package is a group of similar types of classes, interfaces and sub-packages. All
we need to do is put related classes into packages. After that, we can simply write an
import class from existing packages and use it in our program. A package is a
container of a group of related classes where some of the classes are accessible are
exposed and others are kept for internal purpose. We can reuse existing classes from
the packages as many time as we need it in our program.
Package names and directory structure are closely related. For example if a package
name is college.staff.bse, then there are three directories, college, staff and bsc such
that bsc is present in staff and staff is present inside college. Also, the directory
college is accessible through CLASSPATH variable, i.e., path of parent directory of
college is present in CLASSPATH. The idea is to make sure that classes are easy to
locate.
Package naming conventions : Packages are named in reverse order of domain
names, i.e., org.college.practice. For example, in a college, the recommended
convention is college.tech.bsc, college.tech.bcom, college.art.bcca, etc.
Subpackages: Packages that are inside another package are the subpackages.
These are not imported by default, they have to imported explicitly. Also, members of
a subpackage have no access privileges, i.e., they are considered as different package
for protected and default access specifiers.
Advantage of Package
Syntax:
Example:
To Compile: javac -d . Simple.java
To Run: java mypack.Simple
Another Way:
1. Create a folder with same as package name and save the package program.
2. Compile the program as javac class_name.java
3. Package folder and main java class file put it into the single folder.
4. Then compile the main java program and run the java program.
Compile: javac main_class.java
Run: java main_class
Types of packages:
Package in java can be categorized in two form, built-in package and user-defined
package.
Built-in Packages:
These packages consist of a large number of classes which are a part of Java API.
Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive
data types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked
List, Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.
These are the packages that are defined by the user. First we create a directory
myPackage (name should be same as the name of the package). Then create the
MyClass inside the directory with the first statement being the package names.
Example:
B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:
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 Package
Subpackage in java
Package inside the package is called the subpackage. It should be created to
categorize the package further. 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 operation, Socket and ServerSocket classes are for networking
Example of Subpackage
package com.java.core;
class Simple{
public static void main(String args[]){
System.out.println("Hello subpackage");
}
}
To Compile: javac -d . Simple.java
To Run: java com.java.core.Simple
Important points:
Byte Streams: These handle data in bytes i.e the byte stream classes read/write
data. Using these you can store characters, videos, audios, images .. etc.
Character Streams – These handle data in Unicode. Using these you can read and
write text data only.