What is ‘package’?

To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages.  Java classes always exist in a class package. There is a default package which doesn’t have a name. Java core API is made up of several packages. Class names in a package are qualified by the package name e.g Math class has the fully qualified name as java.lang.Math

Uses of packages

The names of your classes and interfaces won’t conflict with the names in other packages because the package creates a new namespace.
Names used for classes in one package will not interfere with the names of classes in another package

Creating packages

  • Add a package statement as the first statement in your source file containing the class definition.
  • Only comments and blank lines are allowed to precede the package statement
  • A package statement consist of the keyword package followed by the package name terminated by a semicolon.
  • You can specify a package name as a sequence of names separated by periods.
  • If you do not use a package statement, your type ends up in an unnamed package. Use an unnamed package only for small or temporary applications
  • Packages are intimately related to the directory structure in which they are stored
  • Class files must be in a directory named by the package

Placing a class in a Package

To place a class in a package, we write the following as the first line of the code (except comments)

                package <packageName>;

                package myownpackage;

Example:

package SchoolClasses;
public class StudentRecord {
  private String name;
  private String address;
  private int age;
  ...
}

Using classes in a Package

To use a public package member (classes and interfaces) from outside its package, you must do one of the following

  • Import the package member using import statement
  • Import the member’s entire package using import statement
  • Refer to the member by its fully qualified name (without using import statement)

Importing Packages

To be able to use classes outside of the package you are currently working in, you need to import the package of those classes.

// Importing a class
import java.util.Date;
// Importing all classes in the java.util package
import java.util.*;
//Using fully qualified names
java.util.Date x = new java.util.Date();

Package & Directory Structure

Packages can also be nested. In this case, the Java interpreter expects the directory structure containing the executable classes to match the package hierarchy.

There should be same directory structure, ./myowndir/myownsubdir/myownpackage directory for the following package statement

          Package myowndir.myownsubdir.myownpackage;  

Example

// in the Rectangle.java file
package graphics;
public class Rectangle() {
  ...
}

Place the source file in a directory whose name reflects the name of the package to which the class belongs  …..\graphics\Rectangle.java

Directory Structure of Java Source Files

Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name

Example

  class name:                           graphics.Rectangle
  pathname to source file:              graphics/Rectangle.java
  pathname to the class file:           graphics/Rectangle.class

However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as:

<path_one>\sources\com\example\graphics\Rectangle.java
<path_two>\classes\com\example\graphics\Rectangle.class

By doing this, you can give the classes directory to other programmers without revealing your sources. You also need to manage source and class files in this manner so that the compiler and the Java Virtual Machine (JVM) can find all the types your program uses.

Setting classpath

Suppose we place the package schoolClasses under the C:\ directory.

  1. We need to set the classpath to point to that directory so that when we try to run it, the JVM will be able to see where our classes are stored.
  2. We type this at the command prompt, C:\schoolClasses> set classpath=C:\
  3. After setting the classpath, we can now run our program anywhere by typing, C:\schoolClasses> java schoolClasses.StudentRecord
  4. Take note that you can set the classpath anywhere. You can also set more than one classpath, we just have to separate them by ;(for windows) set classpath=C:\myClasses;D:\;E:\MyPrograms\Java

Tagged with: ClassesJAVAObject Oriented
 

4 Responses to Classes and packages in Java

  1. i want to be expart in progaming.so help me in the learning.Thanks a lot.

  2. Jonalene says:

    I want to learn more about languages. I hope there’s someone here could teach me about it.

    chat you soon on google plus.
    my email is jonalene.edio@gmail.com

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.