Java Packages

Java packages are uniquely named collections of classes. The primary reason for grouping classes in packages is to avoid possible name clashes.

Packages are useful for the following reasons:

  • Allowing organization of classes into units
  • Removing naming collision
  • Providing better protection
  • Can be used to identify classes

Each class written in Java is part of a package.

Declaring the package to which the class belongs:

package vehicle;

public class Car {
  // fields and methods
}

The package must be the first statement in a Java file.

If we try to declare the package elsewhere, we will get a compiler error:

package vehicle; // OK

public class Car {
package vehicle;  // DOES NOT COMPILE
// fields and methods
}

package vehicle; // DOES NOT COMPILE

If we do not specify a package while creating a class, it will belong to the default package. 

We may have classes with the same name in our application, but they must be in different packages.

Java Packages naming

Package names should be written in all lower cases to avoid conflict with the names of classes or interfaces.
Only alphabets, numbers, and an underscore are allowed in naming the packages.
The package name should start with an alphabet or underscore but not a number.

If the name contains multiple words, it should be separated by dots (.) such as java.util, java.lang.

Import package

If we want to use a class from another package, we must import it using keyword import.

If we try to use a class, without importing it, we’ll get a compiler error:

package vehicle;

class Car {

  public void printRandomNumber() {
    Random r = new Random(); // DOES NOT COMPILE
    System.out.println(r.nextInt(10)); // DOES NOT COMPILE
  }
}

It’s because Java doesn’t know where to look for the Random class. So it needs us to tell it which packages to look in to find the code.

This will compile:

package vehicle;

import java.util.Random;

class Car {
    
  public void printRandomNumber() {
    Random r = new Random(); // OK
    System.out.println(r.nextInt(10)); // OK
  }
}


Also, we can use a wildcard to import all classes and interfaces from the package:
import java.util.*

Static import

Static import is used to import static fields/methods of a class. It allows access to the static members of a class without the class qualification.

To access the static members of a class, you have to use the full class name that contains it.

See the following example:

class Test{
   
  public static void main(String[] args) {
    System.out.println(Math.max(2, 10));
  }
}
Output: 10
 
We call the max() method from the Math class by using the class name.

If we import this static method, we can use it without having to qualify it:

import static java.lang.Math.max;

class App {
 
  public static void main(String[] args) {
    System.out.println(max(2, 10));
  }
}
Output: 10
 
We can also use a wildcard, to import all static members from the class:
import static java.lang.Math.*;

class App {

  public static void main(String[] args) {
    System.out.println(max(2, 10));
    System.out.println(min(2, 10));
  }
}
Output: 10 2
 
That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *