What is Java Package ?

Java Package join a group of related classes, sub packages and interfaces. Java Package are used for:

  • Preventing naming conflicts.

  • Providing controlled access

  • It also considered as data encapsulation.

  • How to Create a Package

    Choose name of the package and destination folder.

    The package statement must be in the first line .Should be one package statement in each source file.

    Create Package Syntax
    javac -d folder_name file_name.java

    Java Package Example

    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();   


    © copyright 2017-2022 Completedone pvt ltd.