Definition of Package in Java

  • A package in Java is a collection of similar types of related sub-packages, classes, interfaces, enumerations, annotations, etc. in separate bundles that provide easy access, security, protection, and namespace (storage place) management.

Features/Characteristics 

  • It behaves like a container that mainly stores similar types of related sub-packages, classes, and interfaces.
  • We can reuse the methods of a class & interface of the package or sub-package many times as per requirements in our Java program by simply importing those packages once as the first statement or beginning of the Java program using the import keyword.
  • When a package creates another package inside it, is called the subpackage. It is normally created to categorize the package further for better management.
  • It shows encapsulation features in which various related sub-packages, classes & interfaces are encapsulated in separate bundles.
  • A user-created class in Java is always either a default package (when the package is not defined) or maybe a part of any user-defined package.
  • If no package is specified/defined for a Java program, then that Java program is stored automatically into a special unnamed package called the default package.
  • All system-defined and user-defined classes/interfaces in a Java file must be a part of a package either a defined package or a default package.
  • A user-created class in Java is always either a default package (when the package is not defined) or maybe a part of any user-defined package.
  • Import keyword: The import keyword is used to include a class or a package/multiple classes from the package library needed in the program. for example –
import java.util.Scanner;    //Import a single-class Scanner.
import java.util.*;     //Import all the classes of the util package.
  • Package Class: A package class is a special class in Java that has different methods to get information about the specification and implementation of a package. Examples of some common methods are getName(), getImplementationTitle(), getImplementationVendor(), getImplementationVersion(), etc.

Rules/Conventions regarding a package

  • There must be no more than one public class per package file and it must be saved by the public class name. If we want to put two public classes in a package (which is practically not needed), put two Java source files containing one public class each, with the same package name. For example: –
package mypackage;  
public class First
{
Save as = First.java 
package mypackage;  
public class Second
{
}  
Save as = Second.java
  • The ‘package’ keyword is used to create a new user-defined package at the first line of the program.
  • We can use one or more than one import statement (in the beginning/first line of the program) to include different packages in a Java program, as needed.
  • Package declaration should be in the first line and package import should be the second line statement in a Java program.
  • In a class, we can create/declare only one package but a class can import more than one package as per need.
  • The package is saved/stored in a directory or folder having the same name as the package.
  • Classes and interfaces with the same name cannot be put in the same package but they can appear in different packages.

Advantages

  • It is used to organize/store multiple related classes, sub-packages & interfaces mainly so that they can be reused/retrieved/maintained easily and quickly. Thus, they provide reusability of methods multiple times when needed in the program and also support better organization for classes inside packages.
  • It supports access protection i.e. they support control access for selected objects using protected and default keywords.
  • Each package in Java has a unique name and manages the classes and interfaces into a separate namespace/name group for simplification.
  • The package helps to avoid/remove name conflicts or collisions by defining two or more classes with the same name but in different packages.
  • Making searching fast & easy for classes, interfaces, etc.
  • The package supports data encapsulation (or data-hiding) features.

    Disadvantages

    • We cannot pass parameters/arguments in a package.
    • Package programs are comparatively slower and consume more memory during execution than native application programs such as C, C++, etc.
    • The default look and feel of GUI applications written in Java using the Swing/AWT toolkit is slightly different from native applications.

    Types of Packages 

    Packages are of two types –

    (A)System Defined / In-Built / Built-in Package:

    • This package already comes with Java application when we install or with Java API and hence no need to install it separately.
    • Some most common usable built-in packages in Java are –

    (a) java.io : 

      • This package contains classes having input-output-related methods used in the Java programs for input-output statements.

    (b) java.util : 

      • This package contains classes having various utilities-related methods that implement several data structures in the program. e.g.- use of  Date/Time.

    (c) java.lang : 

      • This package contains classes having language support (classes that contain primitive data types, math operations, etc.) related methods used in Java programs for primitive datatype statements.
      • This is the default Java package and is automatically imported into a program and not necessary to import this package in a class.

    (d) java.applet : 

      • This package contains classes having applet-related methods that may be used in Java programs in applet creation.

    (e) java.awt :

      • This package contains classes having graphical user interfaces (like textbox, button, menus, combo box, list box, label, etc.) methods normally required in Java program in interface programming.

    (f) java.net : 

      • This package contains classes having various network-supporting methods used in advanced Java programming for data communication operations.

    (B) User-Defined Package:

    • This package is created by the user as per the requirement of the program and can be used in another Java program.

     Syntax To Compile and Run the Java Package File

        A Java package is compiled in two ways-

    (A) Through the Command Line or DOS mode

    (B) Through IDE (such as Net Beans, Eclipse, etc.)

    (A) Through Command Line or DOS mode:

    (a) To Compile Java Package File/Program

    Syntax=
    (i) c:\ javac MyClass.java    // when folder created manually and then stored java file.
    (ii) c:\javac -d . MyClass.java  
    Here -d (directory) switch or option with . (dot) is used to create a specified directory/ folder as the package name automatically to store/save the generated class file in the same drive or folder. 
    (iii) c:\>javac -d e:\ MyClass.java
    Here, the ‘MyClass.java’ file in C drive makes a class file (in another drive) inside the automatically created ‘mypackage’ directory in E-drive as the package name.
    (iv) c:\>javac -d e:\Program MyClass.java
    Here, the ‘MyClass.java’ file in C drive makes a class file (in a manually created ‘Program’ folder in E drive) inside the automatically created ‘mypackage’ [as the package name] directory inside the ‘Program’ directory of E-drive.

    (b)To Run/Execute/Interpret Java Package File/Program

                    Syntax=C: \Java classfilename              

                    eg.= C:\Java MyClass.class(Enter)

    (B) Through an IDE:

    The IDE may be Net Beans IDE/Eclipse etc. 

    Packages Accessing

    • There are three common ways to access the package for the Java program from outside the package. These are –
      • Using Packagename. all (import package.*;) 
        • In this way, all the classes and interfaces of this particular package are accessible by the Java program but this does not include the classes and interfaces of the subpackages.
        • The import keyword is used to access this package’s classes and interfaces.
        • Syntax: import packagename .* ;
        • Example: import java. io. * ;
      • Using Packagename. classname (import package.classname;)
        • In this way, only the declared class of the package is accessible by the Java program.
        • Syntax: import packagename .classname;
        • Example: import java. util. Scanner ;
      • Using a Fully Qualified/Path Name
        • In this way, only the declared class of the package is accessible.
        • Here, there is no need to import it. But we need to use a fully qualified name whenever we are accessing the class or interface.
        • This method is also used in accessing the classes and interfaces of particular sub-packages easily providing a full path.
        • This type of package accessing is generally used when two packages have the same class name. For example – java.util and java.sql packages contain Date class. Thus, a fully qualified name is – java. util. Date and java. sql. Date.

      Loading

      Categories: Java

      0 Comments

      Leave a Reply

      Your email address will not be published. Required fields are marked *

      This site uses Akismet to reduce spam. Learn how your comment data is processed.