Prev Next

Organizing Code

When you are writing large pieces of code, it is better to keep them in an organized manner. In Java, you do so by including them under different packages. Each package denotes a hierarchy of directory and you put related source files under each directory.

1. Grouping related classes under a package

Lets say Eclipse's workspace is /home/user/workspace. When you created the Geometry project, it created a folder by the same name. i.e. /home/user/workspace/Geometry. This contains src and bin folders. The Java source files goes into this src folder as you create them. And the compiled .class files go into the bin folder. That's how Eclipse organizes the files.

You can create a hierarchical folder structure under the Geometry and distribute the files. Let's say we want to put the main classes Point.java, Line.java, LineSegment.java, Triangle.java, Circle.java, Shape.java in edu.amrita.oop.geometry package.

In Eclipse, it can be done during the creation of the source files by entering edu.amrita.oop.geometry in the Package text box at the top. This will ensure the above the source files go into the folder: edu/amrita/oop/geometry.

  • You will notice the statement package edu.amrita.oop.geometry at the top of each Java class -- meaning the class belongs to this package.

You can maintain the custom exceptions in a different package, say, edu.amrita.oop.geometry.exceptions . Enter this while creating NegativeCoordinateException.java. This will ensure it lands in the folder edu/amrita/oop/geometry/exceptions.

  • You will notice the statement package edu.amrita.oop.geometry.exceptions at the top of the Java class -- meaning this exception class belongs to this package.

Finally, you can move the test drivers to another package, say, edu.amrita.oop.geometry.test . Enter this package while creating the driver classes. This will ensure the driver classes land in the folder edu/amrita/oop/geometry/test.

  • You will notice the statement package edu.amrita.oop.geometry.exceptions at the top of the driver classes -- meaning the driver classes belong to this package.

2. Accessing classes belonging to other packages

Since the classes are now in different packages, accessing them is not straightforward. You need to import a package if you need to use it.

1. In terms of dependency, NegativeCoordinateException.java is independent and does not need other classes. Hence, nothing needs to be done.

2. All the main classes in edu.amrita.oop.geometry may need NegativeCoordinateException. To access them, add the statement import edu.amrita.oop.geometry.exceptions.NegativeCoordinateException; to above the class definition. This will ensure the exception class is accessible.

  • Entering import edu.amrita.oop.geometry.exceptions.*; implies all the classes in the package are accessible.

3. The driver classes need to access both the main geometry classess and the exception. To access them, add the following statements above each driver class definition.

import edu.amrita.oop.geometry.*;
import edu.amrita.oop.geometry.exceptions.*;

Rest of the code remains exactly the same as before.

The default access modifier

Now that we have introduced the Java package concept, it becomes essential to discuss the default access modifier.

You know that the access modifiers private, protected and public are used to control the access to the attributes and methods at different levels. But, what if we don't specify any of the three access modifiers?

In the absence of private, protected or public, default access restriction applies.

    default access = package level access

The attributes and methods with default access can be accessed within the package/folder.

  package edu.amrita.xyz;

  public class X {
    int attrib;   // can be accessed from other classes in xyz folder

    void method() {   //  ---- do ----
      ....
    }
  }

Although the enclosing class is public, attrib and method can only be accessed by classes in the edu/amrita/xyz folder.

This can be extended to the class also. Consdier the following snippet.

  package edu.amrita.xyz;

  class Y {
    ........
    ........
    ........
  }

The class Y can be accessed only from other classes in edu/amrita/xyz folder.