- Containers for classes
- Provides visibility mechanism
- Packages are a way of grouping variety of classes and/or interfaces together
- This grouping is usually done according to functionality
- Thus packages act as containers for classes
Java packages are classified as
–Java API packages (System Packages)
Java API packages (System Packages)
Java APl(Application Program Interface) provides a large numbers of classes grouped into different packages according to functionality. Most of the time we use the packages available with the the Java API. Following figure shows the system packages that are frequently used in the programs.
Provides the API for accessing and processing data stored in a data source (usually a relational database) using the Java programming language.
It is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java.Unlike AWT, Java Swing provides platform-independent and lightweight components.The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
Steps of creating User Defined Packages Java and using them:
- Create a package with a .class file
- set the classpath from the directory from which you would like to access. It may be in a different drive and directory. Let us call it as a target directory.
- Write a program and use the file from the package.
Defining a Package:
- Include a package command as the first statement in a Java source file
- Any classes declared within that file will belong to the specified package
- The package statement defines a name space in which classes are stored
- If package statement is omitted, class names are put into the default package, which has no name
- Java uses file system directories to store packages
- For example, the .class files for any classes we declare to be part of MyPackage must be stored in a directory called MyPackage
public class ClassA
public void displayA()
- Save this file as ClassA.java and store in the subdirectory pkg1
- Compile this file.
- The resultant ClassA.class will be stored in the same directory
public static void main (String args)
ClassAobjA= new ClassA();
- Save this file as PackageTest1.java in the directory of which pkg1 is a subdirectory
- Compile and run the file
- We can create a hierarchy of packages :
- A package hierarchy must be reflected in the file system. For example, a package declared as package java.awt.image; needs to be stored in java\awt\image in a Windows environment.
How does the Java run-time system know where to look for packages that we create?
1.Java run-time system uses the current working directory as its starting point.
Thus, if the package is in a subdirectory of the current directory, it will be found
2.We can specify a directory path or paths by setting the CLASSPATH environmental variable.
Consider the following package specification: package MyPack;
In order for a program to find MyPack, one of two things must be true.
1.Either the program can be executed from a directory immediately above MyPack, or
2.the CLASSPATH must be set to include the path to MyPack
Access modifiers in java
There are two types of modifiers in java: access modifiers and non-access modifiers.
The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.
There are 4 types of java access modifiers:
There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc. Here, we will learn access modifiers.
1) private access modifier
|The private access modifier is accessible only within class.|
Simple example of private access modifier
|In this example, we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is compile time error.|
Role of Private Constructor
|If you make any class constructor private, you cannot create the instance of that class from outside the class. For example:|
Note: A class cannot be private or protected except nested class.
2) default access modifier
|If you don’t use any modifier, it is treated as default by default. The default modifier is accessible only within package.|
Example of default access modifier
|In this example, we have created two packages pack and mypack. We are accessing the A class from outside its package, since A class is not public, so it cannot be accessed from outside the package.|
3) protected access modifier
The protected access modifier is accessible within package and outside the package but through inheritance only.The protected access modifier can be applied on the data member, method and constructor. It can’t be applied on the class.
Example of protected access modifier
In this example, we have created the two packages pack and mypack. The A class of pack package is public, so can be accessed from outside the package. But msg() method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.
4) public access modifier
The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.
Example of public access modifier