Packages in Java

Packages in Java

In this article, I'll explain 1. what packages are, 2. How to create a package in java, 3. And some naming conventions you need to follow!

·

3 min read

What are packages in Java?

In Java, packages are there to encapsulate classes, methods, interfaces, and sub-packages. So, we can use them whenever we want in our programs without writing the same code again. Packages organize all the classes and methods available based on the type/ work that they do. Due to that, we can easily access them.

Types of Packages in Java

  • There are built-in packages as well as you can create user-defined packages. Some of the built-in packages in Java,

            java.lang - It contains classes for primitive types, process, System, Thread.
            java.io  - It contains classes required for input/output operations , file handling.
            java.util - It contains the collections framework , event model , date and time facilities
            java.applet - It provides classes needed to create an applet and the classes an applet uses to 
                                  communicate with it's applet context
            java.awt  -  It contains classes for creating user-interfaces and for creating graphics and 
                                images.
            java.net   - It provides classes for implementing networking applications.
    

Packages are named in the reverse order of domain names. Example: com.google.myClass

Naming conventions

  • Always start with lowercase letters
  • Choose unique package name
  • Companies use their reversed Internet domain name to begin their package names

Subpackages

  • Sub packages are packages present in other packages. Example: lang, util, awt, etc., all these are sub-packages present in the java package.

import java.util.LinkedList; This will import only Linkedlist class of util package import java.util.* This will import all classes in the subpackage util.

Create your package and use it in other programs without implementing again.

Let us create a package having a class named Operations which contains methods like addition, subtraction, multiplication of two numbers by taking double parameters and returning double, finding sum from 1 to n where n is a parameter, finding an nth Fibonacci number. Now we can create a package with the above specifications. I'll explain step by step how to create a package of your own,

Creating a package

creatingpkg1.JPG

creatingpkg2.JPG

Create class Operations

creatingclass.JPG

Create all methods required

package com.google.operations;

public class Operations {
    public static int Add(int a,int b)
    {
        return a+b;
    }
    public static int Subtract(int a,int b)
    {
        return a-b;
    }
    public static int multiply(int a,int b)
    {
        return a*b;
    }
    public static double divide(double a,double b)
    {
        if(b!=0)
        return a/b;
        return -1;
    }
    public static int sum(int N)
    {
        int sumUptoN=0;
        for(int i=1;i<=N;i++)
        {
            sumUptoN+=i;
        }
        return sumUptoN;
    }
    public static int nthFibo(int N)
    {
        if(N==1)
            return 0;
        if(N==2)
            return 1;
        int first=0;
        int second=1;
        int NthFibo=0;
        for(int i=3;i<=N;i++)
        {
            NthFibo=first+second;
            first=second;
            second=NthFibo;
        }
        return NthFibo;
    }
}

Folder structure

folderstructure.JPG

Test functionality of methods in main class

test.JPG

Now delete main class

Folder structure

folderstructure2.JPG

Create jar file

  • Navigate to File/Project Structure/Artifacts
  • Create new Artifact by clicking + symbol--> select JAR--> From modules with dependencies--> Click ok
  • Build project
  • Build Artifacts by navigating to build
  • Now check out in project structure out-->artifacts-->Operations.jar

outf.JPG

  • This is the file that we will be importing in whichever projects we want to use the functionality.

Create a new project named OperationsTest

testope.JPG

  • Make sure to change base package

Import the package

  • Go to File/Project Structure/Libraries
  • Click on + -> New project library and select the jar file

file.JPG

Capture.JPG

  • click ok

  • import package in the main class too.

Use the package functionality in operationsTest

output.JPG

That's it you were able to create a package of your own and able to use it in your own class in different package.

Like the article if you found it useful and if you are till here, cheers!!