BIG
DATA

JAVA

PACKAGES IN JAVA

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

Most of the times you start with a small project and you intend to put all java files into one single folder. There is no harm in doing it. However if your project grows, you may have to add more java files and putting all into the same folder will be a nightmare. Well Java has a solution to this and that is by using Packages.

A package is a namespace that organizes your classes and interfaces of project. It is a mechanism to group Java classes that are related to each other, into the same package. A Java package is like a folder/directory in a file system. When a Java project grows bigger, it is useful to split the code into multiple Java classes, and the classes into multiple Java packages. When you divide classes into multiple Java packages, it becomes easier to figure out where a certain class you are looking for is.

Advantages of using a package
  • Easy to locate the files and easy to determine what these types are related when proper package name is given.
  • In real life situation there may come a situation where we need to define files of the same name. This may lead to 'name-space collisions'. Packaging helps us to avoid class name collision when we use the same class name as that of others.
  • Packages eases maintenance work by categorizing the classes and interfaces.
  • Packages increases collaboration among developers.
  • Package provides access protection by providing unrestricted access to one another within the package and restrict access for types outside the package.

Creating a Java Package

To create a package, you choose a name for the package and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.

To create a Java package you must first create a source root directory on your hard disk. Once you have created a source root directory you can start adding subdirectories to it. Each subdirectory corresponds to a Java package. You can add subdirectories inside subdirectories to create a deeper package structure.

Now once directories are created, put the Java source file inside a directory matching the Java package you want to put the class in. When you have put your Java source file into the correct directory, you have to declare inside that class file, that it belongs to that Java package.

Following is an example of declaring the package inside Java source file.

package  bpackage;

public class B {
	...
}

The package statement (for example, package bpackage;) must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

Naming a Package

Java packages are always written in lowercase letters to avoid conflict with the names of classes or interfaces.

Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com. In that case, since the domain name of my company is corejavaguru.com I should have a package structure called com.corejavaguru.

Packages in the Java language itself begin with java. or javax.

Using Package Members

All the member types that comprise a package are known as the package members. To use a public package member from outside its package, you must do one of the following:

  • Import the package member
  • Import the member's entire package
  • Refer to the member by its fully qualified name
Importing a Package Member

If a class A needs to use the class B, you must reference class B inside class A. If class A and B are located in the same Java package, the Java compiler will accept references between the two classes. However, if class A and B are located in different Java packages, then class A must import class B in order to use it.

To import a specific member into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement, if there is one. Here's how you would import the class B from the package bpackage.

import bpackage.B;

public class A {
    public static void main(String[] args){
        B bObj = new B();
	//some java code to follow
    }
}

Once imported you can refer to the B class by its simple name as above.

This approach works well if you use just a few members from that package. But if you use many types from a package, you should import the entire package.

Importing an Entire Package

If you need to use many of the classes from a Java package, importing them one at a time results in a lot of import statements. To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character.

import bpackage.*;

Now you can refer to any class or interface in the bpackage by its simple name.

Referring to a Member by Its Qualified Name

You can use a package member's simple name if the code you are writing is in the same package as that member or if that member has been imported.

However, if you are trying to use a member from a different package and that package has not been imported, you must use the member's fully qualified name, which includes the package name. Here is the fully qualified name for the class B declared in bpackage.

bpackage.B

You could use this qualified name to create an instance of class B:

bpackage.B bObj = new bpackage.B();

Note: When a name is used repetitively, however, typing the name repeatedly becomes tedious and the code becomes difficult to read. As an alternative, you can import the member or its package and then use its simple name.

Static import

Static import is used to import static members of a class. We all know that static members are referred in association with its class name outside the class. Using static import, it is possible to refer to the static member directly without its class name.

Read more on static import here.

Note:
  • At most one package declaration can appear in a source file.
  • The package declaration must be the first statement in the unit.