Java Packages

Java Package Java Packages

Packages are important in java. You will create lots of package during application development. But first, we need to understand, what is the java package. How we can create one and what is the purpose, benefit of using packages in java. We will discuss each of them in this tutorial.

Let’s first understand, What is Package ?

Package in java is used to group multiple classes and interfaces together. 

To understand package, let’s take a real world example.

We use and listen about package in our day to day life. Almost everybody use online shopping to order something online. What we do, we choose multiple things like cloths, daily used products, electronics etc. Then company creates a package of different things to deliver it to a particular name.

Why they create package of different things, they can even send them one by one. But think about it, millions of customers order millions of thing online. If they will start sending them one by one, you can imagine how long it will take to deliver your next product.

They create package because it will be easy for them to deliver multiple products at the same time to the same customer. Which will save time of the customer and money of Company.
 
Same way, we create packages in our day to day projects. Because It will be easy for us to remember, maintain, lookup different classes and interface.

What are the advantages of using Packages in java

  • Java package allows you to categorize your classes and interfaces. There many be many classes in your application. If you categorize them, it will be easy for you to lookup those classes and interface for particular category.
  • Java package provides you access protection.
  • There can be situation where you need to create a class which is already created in different package. Java package remove the naming collision.
  • One of the best advantage is that, it provides you the code re-usability.

Things to remember before creating and using any package in Java

  • Package name as per java convention, should be small.
  • Package should always be your first statement. When you write your class inside any package, keep in mind that package name should come before any other statement in java.
  • You can write only one package statement. There can not be multiple package statement in your java class.
  • If you don’t put your class inside any package. Means, if you don’t want to create any package. Then, your class will be placed in default package.

How to create a package in Java

You need to follow below steps in order to create and execute any Java program inside package. 

Step 1 : Create File

Create a java file as below with the name PackageDemo.java :



package com.mypackage;

class PackageDemo {

 public static void main(String arg[]) {

 System.out.println("Inside package.");

 }

}

Step 2 : Compile Java File

You need to run the below command in order to compile your java file.
Note*: When you will compile your java file, your package will automatically be created to the given location as you can see below.



C:\your_directory>javac -d . PackageDemo.java

javac – Your java compiler.
-d – used to provide location where you want to keep and access your package.
. – Means your current directory.

 
Step 3 : Run your java program

You can run your java program in different ways.

Method 1 : use full package structure along with class name as below.
C:\directory>java com.mypackage.PackageDemo
Method 2 : Go inside your package and run the current class.
C:\directory\com\mypackage>java PackageDemo


How to use a package (Import)

After creating packages, We need to use them. We can use package in our class by using “import” keyword in java. Import keyword tell JVM to load these classes before compiling the current class. Because these classes will be required by the current class. So these classes will be available for you to use.

How to import a package.

You can import package in different ways as below :

  1. Using this statement – com.mypackage.*;
  2. Using this statement – com.mypackage.ClassName;
  3. Using package structure along with Class name in program.

Example of using first method.

This method will load all the classes except static classes from mypackage. “*” is used to load all classes.

package com.different.package;

import com.mypackage.*;

class DifferentPackageDemo {

 public static void main(String arg[]) {

    PackageDemo d = new PackageDemo();
    System.out.println("Inside Different package.");

 }

}

Example of using second method

This method will load only a particular Class from the given package.

package com.different.package;

import com.mypackage.PackageDemo;

class DifferentPackageDemo {

 public static void main(String arg[]) {

    PackageDemo d = new PackageDemo();
    System.out.println("Inside Different package.");

 }

}

Example of using third method

This method is useful when you have same Class name but packages are different, then you can specify which Class you want to use.

package com.different.package;

class DifferentPackageDemo {

 public static void main(String arg[]) {

    com.mypackage.PackageDemo d = new com.mypackage.PackageDemo();
    System.out.println("Inside Different package.");

 }

}

How to use your class anywhere on your system

If you want to use your class file anywhere in your directory or PC. Then you need to set classPath of your directory as below.

Provide the location where your package directory is created. In our case, com\mypackage and the parent directory is packageexample. So provide location upto packageexample.

C:\>set classpath=C:\mydirectory\packageexample;.;
Now you can compile your code any where on your system as below.

C:\directory>java com.mypackage.PackageDemo

Leave a Reply

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