Image for Packing heat with Java packages

Packing heat with Java packages

What's in a package?


In Java there's a concept known as packages. These are like folders but with some benefits! What are these benefits and why would we use them?

Whenever we use some standard class in Java, like Scanner or Math, they are bundled up in a package. Let's look at both Scanner and Math: Scanner is found in the package util in the java package. Math is found in the lang package also in the java package. If we want to use these classes they need to be imported. All the classes in the lang package are imported implicitly in all standard Java programs. That means that Math is always accessible without importing it, but Scanner is not in the lang package so it needs to be explicitly imported or used. This means that the class can either be imported in top of your java-file or it's explicitly addressed when used.

Importing

import java.util.Scanner; // Now it suffice to just use Scanner in a method.


Explicit use

// Inside a method:
java.util.Scanner scanner = new java.util.Scanner(System.in);


Manage access expectations


So packages are a smart way to have similar classes together, but what's also smart with it is that classes within the same package have more access to eachothers methods and fields than classes of other packages. What do I mean by that? Access modifiers!

You might have seen access modifiers like public, protected, and private. There's also a default access modifier. While public means that any other class may access that field or method, private is the opposite. Protected allows access to classes in the same package, or classes inheriting from that class. Default is the access modifier when no other access modifier is used. This allows access only to classes of the same package. The difference from protected is that a class in another package inheriting from a class with default do not have access to its method or field.

An overview of access modifiers.

Creating your own package


The following code describes a Rectangle. It has two fields: width and height, and some methods to interact with them. But of interest here is the first row.


package online.andersbjorkland.shapes;

public class Rectangle {
private double width;
private double height;

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}

public double getArea() {
return height * width;
}
}


The first line of code has to be the package name. Lacking this the file is organised under a nameless package. So in this example the class Rectangle is in the package online.andersbjorkland.shapes. The first couple of packages online and andersbjorkland is a way to distinguish my code from any other Java developer's code. As I have a domain that I control I'm using that to distinguish my code.The top level domain is the first part, then comes my domain-name. Lastly comes the packages to contain my code (shapes).

While it's fine and all to declare the package up top, that's not all it takes to have a functional package. You have to set up the folders as well; yes, packages are folders. So online.andersbjorkland.shapes means that we will have a folder online that containts a folder andersbjorkland which in turn contains a folder shapes.

Java packages are folders

Now that's what's in a package!

Read more about packages in the official tutorial over at Oracle.

On Friday I write about UML and how a Java class can be represented in a UML diagram. See you around then!