Java - Core Java - Packages in Java


This mechanism is the package. The package is both a naming and a visibility control mechanism. You can define classes inside a package that are not accessible by code outside that package. You can also define class members that are only exposed to other members of the same package. This allows your classes to have intimate knowledge of each other, but not expose that knowledge to the rest of the world. A package is a collection of classes, interfaces and sub-packages. A sub-package in turns divides into classes, interfaces, sub-sub-packages, etc.

Learning about JAVA is nothing but learning about various packages. By default one predefined package is imported for each and every JAVA program and whose name is java.lang.*. Whenever we develop any java program, it may contain many number of user defined classes and user defined interfaces. If we are not using any package name to place user defined classes and interfaces, JVM will assume its own package called NONAME package.

Predefine package

Predefined packages are those which are developed by SUN micro systems and supplied as a part of JDK (Java Development Kit) to simplify the task of java programmer.*

This package is used for developing file handling applications, such as, opening the file in read or write mode, reading or writing the data, etc.

(abstract windowing toolkit)

This package is used for developing GUI (Graphic Unit Interface) components such as buttons, check boxes, scroll boxes, etc.


Event is the sub package of awt package. This package is used for providing the functionality to GUI components, such as, when button is clicked or when check box is checked, when scroll box is adjusted either vertically or horizontally.


This package is used for developing browser oriented applications. In other words this package is used for developing distributed programs. An applet is a java program which runs in the context of www or browser.*

This package is used for developing client server applications.


This package is used for developing quality or reliable applications in java or J2EE. This package contains various classes and interfaces which improves the performance of J2ME applications. This package is also known as collection framework (collection framework is the standardized mechanism of grouping of similar or different type of objects into single object. This single object is known as collection object).

This package is used for formatting date and time on day to day business operations.

Defining a Package

To create a package is quite easy: simply 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 you omit the package statement, the class names are put into the default package, which has no name. A user defined package is one which is developed by java programmers to simplify the task of the java programmers to keep set of classes, interfaces and sub packages which are commonly used. Any class or interface is commonly used by many java programmers that class or interface must be placed in packages.

package pack1[.pack2[.pack3……[.packn]…..]];

Here, package is a keyword which is used for creating user defined packages, pack1 represents upper package and pack2 to packn represents sub packages.

For example:
package p1;  statement-1
package p1.p2;  statement-2
The statements 1 and 2 are called package statements.

STEPS for developing a PACKAGE

  1. Choose the appropriate package name, the package name must be a JAVA valid variable name and we showed ensure the package statement must be first executable statement.
  2. Choose the appropriate class name or interface name and whose modifier must be public.
  3. The modifier of Constructors of a class must be public.
  4. The modifier of the methods of class name or interface name must be public.
  5. At any point of time we should place either a class or an interface in a package and give the file name as class name or interface name with extension .

//Creating our first package: File name –

package package_name;
public class ClassOne {
    public void methodClassOne() {
        System.out.println("Hello there its ClassOne");

//Creating our second package: //File name –

package package_one;
public class ClassTwo {
    public void methodClassTwo(){
        System.out.println("Hello there i am ClassTwo");

//Making use of both the created packages: //File name –

import package_one.ClassTwo;
import package_name.ClassOne;
public class Testing {
    public static void main(String[] args){
        ClassTwo a = new ClassTwo();
        ClassOne b = new ClassOne();