What is Java Package

Java Package

Java Package

Package in java organizes your program in a proper folder structure. It encapsulates the group of sub packages, interfaces, and classes in one folder and provides access, protection, and namespace management.

An advantage of a package in Java

  • Packages avoid naming conflicts between the classes. For example, we can store the two classes with the same name in two packages.
  • It makes the searching or locating and usage of the classes, enumeration, annotation, and interfaces much easier so it is very important to use a package, to structure your project.
  • We can consider a package uses for data encapsulation i.e for data-hiding.
  • Packages provide controlled access like protected and default modifiers have package level access

How to create the package

To declare the package we need to use the keyword called package package_name. Package declaration must be the first statement of our program. For example – Package com.Xadmin.Hello. Package name separated by a dot(.) represents the one folder inside another one.

How to compile the package containing the program

To compile the java programs, you have to use -d option as shown below:
javac -d Destination_folder_name file_name.java
 The folder will be created in the specified destination with the provided package name and compiled class files will be placed in that folder.

  • Javac -d . Xadmin.java  here, dot means compile will be done to the current working directory.
  • Javac -d . *.java  here, * means all .java class will get compile on a current working directory
  • Javac -d E:/pack Xadmin.java  here, it will store to E:/pack drive address.

How to run the package program
 Firstly, we have to set the path, if compile-file is present in a different address.
For windows  set classpath=%classpath%;E:\pack. We can run the java program by writing this following syntax:
java com.Xadmin.Hello here, Hello class is present inside the com.Xadmin package.

Sample code to understand the package concept

package com.Xadmin.p1 // here, declaring a package
class Hello
{
 void m1()
 {
 System.out.println(“m1 in the Hello”);
 }
}
class Xadmin
{
 public static void main(String args[])
 {
 Hello h1= new Hello();
 hi.m1();
 }
}

To compile the above program

  • Javac -d . Xadmin.java – it will compile and make .class file in a current working directory.
  • Javac -d .*.java -it will compile all the java class.
  • Javac -d E:/pack *.java – it will store the .class file in the given directory address.

To run the above program

To run firstly, we have to set the path with set classpath=%classpath%; E:/pack then we have to run with
java com.Xadmin.p1.Xadmin here, -d represent the creation of package structure dot(.) represent package structure has to place in the current working directory.

  • When we are compiling the program with Javac -d . Xadmin.java then only Xadmin java class will be compiled.
  • We are compiling the program with Javac -d .*.java then all the java program present in the current working directory will get compiled.
  • If we want to place our package structure in some other directory then provide the location of the directory in the place of the dot(.) like Javac -d E:/pack *.java.

In order to run any package program, we need to provide the fully qualified class name. When we have set the classpath to some directory then whenever we run the program it will search for the .class file in that location only.

How to import the package

If you want to use the class which is available inside the other package then we need to use the import statement. For example-

  • import com.Xadmin.p1.Hello;
  • import com.Xadmin.p2.*;

whenever we are writing the first import statement then from the com.Xadmin.p1 package only class Hello will be imported. If we are writing the second import statement then all the class present inside com.Xadmin.p2 package will be imported.

Types of packages in java

There are two types of packages in java

Types of packages in javaBuilt-in packages in java: Built-in java packages are a part of Java API and these packages contain a large number of classes. Some of the commonly used built-in packages are listed below:

  • java.lang: This built-in package contains language support classes and this is automatically imported to the class.
  • java.io: This package contains classes for input/output operations.
  • java.util: This contains utility classes for implementing data structure.
  • java.applet: Contains classes for creating Applets.
  • java.awt: This package contains classes for creating graphical user interfaces (like buttons, menus etc).
  • java.net: contain classes for supporting and implementing networking operations

User-defined packages in java: These are the packages that are defined by the users in a java class. Firstly, we have to create a directory with a name ( this name should be the same as the name of the package used in a class). Then create your java class in that directory with the first statement as the package name.


Conclusion:

There is no doubt that the java package is one of the most important parts for efficient java programmers and also for better structuring of our project. So we can take a simple conclusion, Package programming in Java is not only upgrading our coding style but also reduce a lot of additional jobs. Happy coding.

READ NOW :- Important classes in java lang package

Leave a Reply

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