bscodinglab

Package

Package :- In Java, a package is a way to organize and group related classes and interfaces together. Packages help you manage and structure your code in a more organized and modular way. Packages are used to avoid naming conflicts, manage large codebases, and provide a clear organization of related classes. They help in categorizing and managing Java classes and make it easier to locate and use them.

A package is a directory that contains a group of related Java classes and interfaces. The package name is like a namespace for the classes within it, which means that two classes with the same name can coexist if they are in different packages.

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.

User-defined packages :-
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.

Important points :-

    1. Every class is part of some package.
    2. If no package is specified, the classes in the file goes into a special unnamed package (the same unnamed package for all files).
    3. All classes/interfaces in a file are part of the same package. Multiple files can specify the same package name.
    4. If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name).
    5. We can access public classes in another (named) package using: package-name.class-name

You can create and use packages in Java need to follow these steps:

Creating a Package :- You can create a package by including a package statement at the top of your Java source files. The syntax for creating a package is as follows.

Example :-

package com.example.mypackage;

Directory Structure :-

Create a Directory Structure: Create a directory structure that mirrors the package structure. For example, if you want to create a package named com.example.myapp, you should create the following directory structure.

Example :-

com/

    example/

        myapp/

Creating Classes in a Package :-

Create Java Classes: Place your Java classes and interfaces within the appropriate directory. For example, a class named MyClass within the com.example.myapp package would be placed in a file like this:

Example :-

// com/example/myapp/MyClass.java

package com.example.myapp;

public class MyClass

{

    public void display()

{

        System.out.println(“This is MyClass in com.example.myapp package”);

  }

    }

Using Classes from a Package :-

Compile and Use Classes: Compile the classes using the javac command. You need to be in the root directory of your package structure and compile the source file like this:

Example :-

javac com/example/myapp/MyClass.java

To use the classes from your package, you must import them in other Java classes that need to access them. For example:

Example :-

import com.example.myapp.MyClass;

public class Main

{

    public static void main(String[] args)

{

        MyClass myObject = new MyClass();

        myObject.display();

 }

    }

Compiling and Running :-

Now, if you compile and run the Main class, it will be able to access the MyClass class from the com.example.myapp package.

Example :-

javac Main.java

java Main

 An Example Of Java Package :-

  • Let’s create a simple example with a package and two classes: MyPackage and MainClass.
  • The package is named com.example.mypackage, and it contains the MyPackage class.
  • We will use the MainClass to demonstrate how to use the MyPackage class.

Class 1 :-

// MyPackage.java

package com.example.mypackage;

public class MyPackage

{

    public void displayMessage()

{

       System.out.println(“Hello from MyPackage class!”);

}

    }

Class 2 :-

// MainClass.java

import com.example.mypackage.MyPackage;

public class MainClass

{

    public static void main(String[] args)

{

        MyPackage myObj = new MyPackage();

        myObj.displayMessage();

 }

     }

Directory :-

com

├── example

│   └── mypackage

│       ├── MyPackage.java

│       └── MainClass.java

Output :-  Hello from MyPackage class!