User Defined Packages - Details about User Defined Packages in Java

A software development is a team work where a number of developers will be engaged in the development of different modules. All these modules will be integrated to make the final product. In such a situation, it is necessary to identify the application developed by an individual in the team and it is also necessary to avoid name duplication.

Java is one of the most widely used programming languages in the world and it is highly user-friendly too. Java provides support to identify an application developed by an individual in the team and also to avoid name duplication. This is possible with user defined packages in Java.

To create user defined packages in java is very easy and it involves just three steps.

Steps of Creating User Defined Packages in Java

  1. Create a package with a .class file
  2. Set a classpath from the target directory. ( A target directory is the directory from which you would like to access it)
  3. Write a program using files from the packages

Create User Defined Packages in Java

Assume that “C:\Snr” is the target directory and it will here the packages will be created.

package forest;

import java.util.*;

public class Tiger

{

public void getDetails(String nickName, int weight)

{

System.out.println(“Tiger nick name is ” + nickName);

System.out.println(“Tiger weight is ” + weight);

}

}

The keyword “Package” followed with the package name can create a package. One should follow the below given order of statements while creating user defined packages in java.

  • Package Statement
  • Import Statement
  • Class Declaration

Any changes in the above order will result in a compilation error.

When code is ready, compilation with package notation can be started. -d compiler options are used here. An example is given below;

C:\snr > javac -d . Tiger.java

In this example a new folder called “Forest” will be created and the class “Tiger” will be put in it.

Once the user defined package is created, the next step is to use it. A classpath should be created for this.

Leave a Reply

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