Overview of Java Package


<aside> <img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents

</aside>

<aside> 💡

  1. Java Package </aside>

Java Package


  1. Java Package

    1. java package is a group of similar types of classes, interfaces and sub-packages.
    2. Package in java can be categorized in two form, built-in package and user-defined package.
    3. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
    4. Advantage of Java Package
      1. Java package is used to categorize the classes and interfaces so that they can be easily maintained.

      2. Java package provides access protection.

      3. Java package removes naming collision.

        image.png

  2. Simple example of java package

    1. The package keyword is used to create a package in java.

      //save as Simple.java  
      package mypack;
      
      public class Simple {
          public static void main(String args[]) {
              System.out.println("Welcome to package");
          }
      }
      
    2. How to compile java package

      1. If we are not using any IDE, you need to follow the syntax given below:

        javac -d directory javafilename
        
      2. For example

        javac -d . Simple.java
        
      3. The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).


  3. How to run java package program

    1. We need to use fully qualified name e.g. mypack.Simple etc. to run the class.

  4. Compilation and Execution

    1. To Compile:

      javac -d . Simple.java
      
    2. To Run:

      java mypack.Simple
      
      *Output:Welcome to package*
      
    3. The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.


  5. How to access package from another package?

    1. There are three ways to access the package from outside the package.

      import package.*;
      
      import package.classname;
      
      1. fully qualified name.
    2. Using packagename.*

      1. If we use package.* then all the classes and interfaces of this package will be accessible but not sub packages.

      2. The import keyword is used to make the classes and interface of another package accessible to the current package.

      3. Example of package that import the packagename.*

        //save by A.java  
        package pack;
        
        public class A {
          public void msg() {
            System.out.println("Hello");
          }
        }
        
        //save by B.java  
        package mypack;
        
        import pack.*;
        
        class B {
          public static void main(String args[]) {
            A obj = new A();
            obj.msg();
          }
        }
        
      4. Output:

        Hello
        
    3. Using packagename.classname

      1. If we import package.classname then only declared class of this package will be accessible.

      2. Example of package by import package.classname

        //save by A.java  
        
        package pack;
        
        public class A {
          public void msg() {
            System.out.println("Hello");
          }
        }
        
        //save by B.java  
        package mypack;
        
        import pack.A;
        
        class B {
          public static void main(String args[]) {
            A obj = new A();
            obj.msg();
          }
        }
        
        *Output:Hello*
        
    4. Using fully qualified name

      1. If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.

      2. It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.

      3. Example of package by import fully qualified name

        //save by A.java  
        package pack;
        
        public class A {
          public void msg() {
            System.out.println("Hello");
          }
        }
        
        //save by 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*
        
    5. Note: If we import a package, sub packages will not be imported.

    6. If we import a package, all the classes and interface of that package will be imported excluding the classes and interfaces of the sub packages. Hence, we need to import the sub package as well.

    7. Note: Sequence of the program must be package then import then class.

      image.png


  6. Sub package in java

    1. Package inside the package is called the subpackage. It should be created to categorize the package further.
    2. An example, Sun Microsystem has defined 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 etc and so on. So, Sun has subcategorized the java package into subpackages such as lang, net, io etc. and put the Input/Output related classes in io package, Server and ServerSocket classes in net packages and so on.
    3. Example of Subpackage
    4. To Compile:
    5. To Run: