Java Package Example

Category: Java   Tags: Java, Java Basic, Java Package

A Java package is used to keep related component separate and more meaningful way. A Java package is just like a directory that keep all your Java classes. A package has below syntax:

                          package packageName;
                        

Keyword package is used to defined a package. packageName is package name. More than one file can include the same package statement.

You can create a hierarchy of packages. To do so, simply separate each package name from the one above it by use of a period. The general form of a multileveled package statement is shown here:

                          package package1[[.package2][.package3]];
                        

After package keyword at least one package name is mandatory. An example of package hierarchy is com.services.db. In this case all Java file using this package should be stored inside the com/services/db directory.

PackageExample.java

                          package com.tutorial.javabasic;

                          class A {
                              public void test() {
                                  System.out.println("test method called");
                              }
                          }

                          public class PackageExample {

                              public static void main(String s[]) {
                                  A a = new A();
                                  a.test();
                              }
                          }
                        

Here, the Java file PackageExample.java is stored inside the com/tutorial/javabasic directory.

Now when you compile this file using javac com.tutorial.javabasic.PackageExample command, generated .class file should also be the same package.

To run this class use command java com.tutorial.javabasic.PackageExample. Remember, the class PackageExample is part of com.tutorial.javabasic.PackageExample therefore you can't run this class just as java PackageExample

Access Control

For a detail discussion refer to Java access control page.

Importing Package

If your Java program need to use some other class defined inside different package, use import statement. In a Java source file, import statements occur immediately following the package statement (if any) and before any class definitions. It has below syntax:

                          import package1[.package2][.package3].(className|*);
                        

If you are using className, then that particular class only will be imported. If you use * then entire package will be imported.

You have to import every package or class that you want to use. java.lang package is implicitly imported by the compiler for all programs. It means, writing the statement import java.lang.*; is not mandatory.

import statement is optional. Instead you can also use fully qualified name, that is full package hierarchy. For example:

                          import java.util.*;
                          class MyList extends ArrayList {}

                          class MyList extends java.util.ArrayList {}

                        

Here both class statement are same. First one is using import statement, second one is using fully qualified package name.

Best practice is to write a fully qualified package name at the top of source file, for example in case of above code it should be like:

                          import java.util.ArrayList;
                          class MyList extends ArrayList {}
                        

If a class with the same name exists in two different packages that you import using the *, the compiler will remain silent, unless you try to use one of the classes. Once you use, you will get a compile-time error and have to explicitly name the class specifying its package.