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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago