Java packages
Start course
1h 58m

In this course, we will learn the concepts of Java EE 7 with a focus on Java Basics.

Learning Objectives

  • What Java classes, Executable Java, Access Modifiers are and how they work

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers


  • Have at least 2 years of Java development experience 

Hello there. In this section, you'll learn what Java packages are and how to create them. You'll use the import statement which enables you to use simple names for classes and interfaces defined in separate packages. Why do you think we need packages? First, answer this question. Do you remember having known more than one Ahmed, Paul, Anu, or John to date? Harry knows more than one Paul, six to be precise, whom he categorizes as managers, friends, and cousins. These are sub categorized by their location and relation as shown in figure. Similarly, you can use packages to group together a related set of classes and interfaces. Packages also provide access protection and name space management. You can create separate packages to define classes for separate projects such as android games and online healthcare systems. Further, you can create sub packages within these packages such as separate subpackages for GUIs, database access, networking, and so on. Programming languages like C, pass on the name of a class as a command line argument to the main method. 

Java doesn't do so. This is a simple but important point. A practical tip here. In real life projects, you will never work with an unpackaged class or interface. Almost all organizations that develop software have strict package naming rules which are often documented. Packaged classes are part of a named package, a name space, and they're defined as being part of a package by including a package statement in a class. All classes and interfaces are packaged. If you don't include an explicit package statement in a class or an interface, it's part of a default package. You can define which classes and interfaces are in a package by using the package statement as the first statement in your class or interface. Here's an example. The class in the previous code defines an exam question class in the certification package. You can define an interface multiple choice in a similar manner. The name of the package in the previous examples is certification. 

You may use such names for small projects that contain only a few classes and interfaces, but it's common for organizations to use sub packages to define all their classes. For example, if the folks at Oracle define a class to store exam questions for a Java associate exam, they might use the package name The package name follows a package naming convention recommended by Oracle and shown in the table. A few important rules about packages. Per Java naming conventions, package names should all be in lowercase. The package and subpackage names are separated using a dot. Package names follow the rules defined for valid identifiers in Java. For packaged classes and interfaces, the package statement is the first statement in a Java source file, a .java file. The exception is that, comments can appear before or after a package statement. 

There can be a maximum of one package statement per Java source code file, .java file. All the classes and interfaces defined in a Java source code file will be defined in the same package. There's no way to package classes and interfaces defined within the same Java source code file in different packages. Note, a fully qualified name for a class or interface is formed by prefixing its package name with its name separated by a period. The hierarchy of the packaged classes should match the hierarchy of the directories in which these classes and interfaces are defined in the code. For example, the class exam question in the certification package should be defined in a directory with the name certification. The name of the directory, certification, and its location are governed by the following rules. For the package example shown in figure, note that there isn't any constraint on the location of the base directory in which the directory structure is defined. The import statement enables you to use simple names instead of using fully qualified names for classes and interfaces defined in separate packages. Let's work with a real-life example. 

Imagine your home and your neighbor's office, living room and kitchen within your home can refer to each other without mentioning that they exist within the same home. Similarly in an office, a cubicle in a conference hall can refer to each other without explicitly mentioning that they exist within the same office, but home and office can't access each other's rooms or cubicles without stating that they exist in a separate home or office. This situation is represented in figure. To refer to the living room in cubicle, you must specify its complete location as shown in left part of the figure. As you can see in this figure, repeated references to the location of the living room make the description of living room look tedious and redundant. To avoid this, you can display a notice in cubicle that all occurrences of living room refer to living room in home and therefore use its simple name. Home and office are like Java packages. In this notice is the equivalent of the import statement, shows the difference in using fully qualified names and simple names for home in cubicle. Let's implement the previous example in code where classes, living room, and kitchen are defined in the package home and classes, cubicle and conference hall are defined in the package office. This class cubicle uses is associated to the class living room in the package home as shown in figure. 

An important note here, the import statement doesn't embed the contents of the imported class in your class, which means that importing more classes doesn't increase the size of your own class. It lets you use the simple name for a class or interface defined in a separate package. It's possible to use a packaged class or interface without using the import statement by using its fully qualified name. This approach can clutter your code if you create multiple variables of interfaces and classes defined in other packages. Use this approach sparingly in actual projects. For the exam, it's important to note that you can't use the import statement to access multiple classes or interfaces with the same names from different packages. For example, the Java API defines the class date in two commonly used packages, java.util and java.sql. To define variables of these classes in a class, use their fully qualified names with the variable declaration. An attempt to use an import statement to import both these classes in the same class will not compile. Code to import classes with the same name from different packages won't compile. 

You can import either a single member or all members classes and interfaces of a package using the import statement. Examine the following code for class annual exam. By using the wildcard character and asterisk, you can import all of the public members, classes, and interfaces of a package. Compare the previous class definition with the following definition of the class annual exam. Unlike in C or C++, importing a class doesn't add to the size of a java.class file. An import statement enables Java to refer to the imported classes without embedding their source code in the target .class file. When you work with an IDE, it may automatically add import statements for classes and interfaces that you reference in your code. What happens if you don't explicitly package your classes or interfaces? In that case, they're packaged in a default no name package. This default package is automatically imported in the Java classes and interfaces defined with the same directory on your system. For example, the classes person and office, which are not defined in an explicit package, can use each other if they are defined in the same directory. 

A class from a default package can't be used in any named package class regardless of whether they are defined within the same directory or not. You can import an individual static member of a class or all its static members by using the import static statement. In this code, the class exam question defines a public static variable named Marks and a public static method named Print. The variable, Marks, can be accessed in the class annual exam using the import statistics statement. The order of the keywords import and static can't be reversed. To access all public static members of class exam question in class annual exam, you can use an asterisk with the import static statement. Because the variable marks and method print are defined as public members, they are accessible to the class Annual exam using the import static statement. These wouldn't be accessible to the class Annual exam if they were defined using any other access modifiers. The accessibility of a class, an interface, and their methods and variables are determined by their access modifiers which are covered in the next section. So that's it. In this lesson, we talked about Java packages. Hope to see you in our next lesson. Have a nice day.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics