Categories: Java

Packages in Java

In Java, a package is a container for a collection of classes, sub-packages, and interfaces. The package keyword is used to create a package in java.

Packages are used for the following purposes:

  • Keeping naming conflicts at bay. For example, in two packages, college.staff.cse.Employee and college.staff.ee.Employee, there could be two classes named Employee.
  • Making it easier to search for, locate, and use classes, interfaces, enumerations, and annotations
  • Controlling access: package-level access control is available in both protected and default modes. Classes in the same package and its subclasses can access a protected member. Only classes in the same package have access to a default member (which has no access specifier).
  • Packages are a type of data encapsulation (or data-hiding).
Java package example:
//save as Simple.java  
package mypack;  
public class Simple{  
 public static void main(String args[]){  
    System.out.println("Welcome to Coderzpy package");  
   }  
}  

What is the procedure for compiling a Java package?
If you are not using an IDE, you must use the following syntax:

javac -d directory javafilename

As an example,

javac -d . Simple. java

The -d switch indicates where the created class file should be saved. You can use any directory name, such as /home (in Linux), d:/abc (in Windows), and so on. You can use it if you wish to keep the package in the same directory (dot).

To run the java package program:

To Compile: javac -d . Simple.java

To Run: java mypack.Simple

Output:

Welcome to Coderzpy package
Accessing a package from another:


There are three ways to get into the package from the outside.

  • import package.*;
  • import package.classname;
  • fully qualified name.
Example: using import package.*;
//save by A.java  
package pack;  
public class A{  
  public void msg(){System.out.println("Hello Coderzpy");}  
}  
//save by B.java  
package mypack;  
import pack.*;  
  
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
}  
Output:
Hello Coderzpy
Example: using import package.classname;
//save by A.java  
  
package pack;  
public class A{  
  public void msg(){System.out.println("Hello Coderzpy");}  
}  
//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 Coderzpy
Example: using a fully qualified name.
//save by A.java  
package pack;  
public class A{  
  public void msg(){System.out.println("Hello Coderzpy");}  
}  
//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 Coderzpy
Types of Packages:
1) Built-in Packages:

These packages contain a huge number of classes that are included in the Java API. The following are some of the most often used built-in packages:

  • java.lang: This package contains language-specific classes (e.g classed which defines primitive data types, and math operations). This package will be imported automatically.
  • java.io: This package contains classes that handle input and output operations.
  • java.util: This package contains utility classes that provide data structures such as Linked Lists, Dictionary, and Date/Time operations.
  • java.applet: This package contains Applet-related classes.
  • java.awt: Contains classes for implementing graphical user interface components (like buttons, menus, etc).
  • java.net: This package contains classes that help with networking.
2) User-defined packages:

These are the packages that the user has defined. Refer to the above-given examples with the package pack and it’s class A.

Note: also read about the instanceof Operator in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

11 hours ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

3 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago