Home » Java programming language

Packages in Java



Learn: In this article, we will learn about different packages in java programming language.
Submitted by Shivangi Jain, on May 01, 2018

Packages

Packages in Java is simply a mechanism to encapsulate (i.e. to put in a short and concise form) a group of classes,interfaces,enumerations, sub packages, etc. In real world, application is developed in such a manner so that we can easily maintain each module. To create package is simply use package keyword with name of the package at first statement in the program.

There are two type of Packages that are found in java,

  1. User defined packages
  2. In Built packages

1) USER DEFINED PACKAGES

The Packages that are created by the user to differentiate between the classes and the interfaces that are made in their projects are user defined packages.

2) IN-BUILT PACKAGES

The Packages that are the part of java API’s and includes variousclasses, interfaces, sub packages that are already defined in it are in-built packages. These packages are also known as the Predefined packages.

There are some packages that exists in java, they are:

  • java.lang: uses to bundles the fundamental classes.
  • java.io: classes for input , output functions are bundled in this package.
  • java.util: classes which are implemented in data structure for date and time operations are bundled here.
  • java.applet: bundles classes for making applets .
  • java.net: bundles the classes for supporting network operations.

These all are in-built packages that are commonly used.

MERITS of packages in java

  1. By the use of packages in java, it becomes easy to search and locate any class, annotation, enumeration etc.
  2. Naming conflict can be prevented that are occurred in between the different classes by the use of java packages.
  3. Java packages renders protection.
  4. Most of programming tasks are done by the API’s classes and Packages, which minimize the number of lines that are written within the piece of code.
  5. Reduction in execution time i.e. execution time is less.
  6. Uses less memory space.
  7. Improved performance.

Steps for creating a user defined package:

  1. Package program’s first statement should be the package statement.
  2. Class modifier must we public so that the class and methods can be used outside the program.
  3. Only one public class or only one public interface are used in package program while any number of normal classes are used in it.
  4. It should contain any main class not the main () in it.
  5. Constructor modifier must be Public.
  6. Method modifier of class or interface must be public.
  7. The package program should be save either with public class name or a public interface name.
    //Sum.java
    //save package with 'public' classname

    //first statement is package
    package OurPackage

    //class modifier must public
    public class Sum
    {
	    //constructor modifier must public.
	    Public Sum()
	    {
		    System.out.println("Sum class constructor");
	    }
	
	    //method modifier must public.
	    Public void show()
	    {
		    System.out.println("Sum class method");
	    }
    }





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.