Digital Edition

SYS-CON.TV
Java Basics: Lesson 11, Java Packages and Imports (Live Video Education)
Lesson 11 in the Hugely Popular "Java Basics" Series by JDJ Editorial Board Member Yakov Fain

Java comes with thousands of classes that are organized in packages (similar to files and directories on you disk). Some packages include classes responsible for drawing, while other have classes for the Internet access, and so on. For example the class String is located in the package called java.lang, and the fully qualified name of this class is java.lang.String.

The Java compiler only knows where to find classes that are located in the package java.lang, but there are many other packages with useful classes, and it's your responsibility to let the compiler know where the classes that are used in your program live. For example, the package java.io contains classes responsible for input/output operations, while most of the Swing classes live in the following two packages:


javax.swing
javax.swing.event

It would be annoying to write a full class name every time you use it, for example:

javax.swing.JButton myButton = new javax.swing.JButton();
javax.swing.JFrame myFrame = new javax.swing.JFrame();

To avoid this you can use import statements right above the class declaration line, for example:

import javax.swing.JFrame;
import javax.swing.JButton;

class Calculator{
JButton myButton = new JButton();
JFrame myFrame = new JFrame();
}

These import statements allow you to use short class names like JFrame or JButton, and the Java compiler will know where to find them. Please note, that nothing is actually imported into your program: it's just a name resolution mechanism that helps the compiler to find classes and make your program more readable. If your need to use several classes from the same package, you do not have to list each of them in the import statement, just use the wild card. In the following example the asterisk (*) makes all classes from the package javax.swing visible to your program:

import javax.swing.*;

Still, it's better to use separate import statements, so you can see clearly which classes are imported from a particular package.

When programmers work on large projects that have lots of classes, they usually organize them in different packages. For example, one package can have all classes that display graphical windows, while another can contain data access classes.

Let's create a new project called PingPong in the Eclipse IDE. This project will have classes in two packages: screens and engine. Now create a new class PingPongTable and enter the word screens in the field Package:  

Press the button Finish and Eclipse will generate the code that will include the line with the package name.

package screens;

public class PingPongTable {

public static void main(String[] args) {
}
}

By the way, if your class includes the line with the keyword package, you are not allowed to write anything but the program comments above this line.

Since each package is stored in a different folder on a disk, Eclipse creates the folder called screens and puts the file PinPongTable.java there. Check it out - there should be a folder c:\eclipse\workspace\PingPong\screens on your disk with files PingPongTable.java and PingPongTable.class.

Now create another class called PingPongEngine and enter the word engine as the package name. The PingPong project has two packages now:

Since our two classes are located in two different packages (and folders), the class PingPongTable won't see the class PingPongEngine unless you add the appropriate import statement.

package screens;

import engine.PingPongEngine;

public class PingPongTable {

public static void main(String[] args) {
  PingPongEngine gameEngine = new
      PingPongEngine();
}
}

Java packages not only help better organize your classes, but also can be used to hide their classes from the "foreigners" living in other packages. In Java you can use public, private and protected keywords to specify the access level to a particular method or a class. But if you do not use any of these keywords in the method or class declaration, you'll be able to access them only from the classes located in the same package. We've discussed access levels briefly in Lesson 3 of this series.

About Yakov Fain
Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

I'm glad you mentioned the use of interfaces as repositories of constants. While it may not be recommended in the strict sense of why interfaces exist, I've used this technique for years on large projects as a good way of organizing and reusing constants.

So you can use wildcards in the import? Can you use static import for static classes?

Will the other 10 lessons be on SYS-CON.TV?

Is it Yakov Fain who is speaking at the moment? A live Java lesson - cool!

I used this lesson in conjunction with the last one, lesson ten on eclipse IDE and it really helped me




ADS BY GOOGLE
Subscribe to the World's Most Powerful Newsletters

ADS BY GOOGLE

Enterprise architects are increasingly adopting multi-cloud strategies as they seek to utilize exist...
Docker containers have brought great opportunities to shorten the deployment process through continu...
Kubernetes is a new and revolutionary open-sourced system for managing containers across multiple ho...
Leading companies, from the Global Fortune 500 to the smallest companies, are adopting hybrid cloud ...
In his session at Cloud Expo, Alan Winters, U.S. Head of Business Development at MobiDev, presented ...
"We work around really protecting the confidentiality of information, and by doing so we've develope...
In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the te...
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the bu...
When shopping for a new data processing platform for IoT solutions, many development teams want to b...
In IT, we sometimes coin terms for things before we know exactly what they are and how they’ll be us...
Detecting internal user threats in the Big Data eco-system is challenging and cumbersome. Many organ...
In his session at 20th Cloud Expo, Scott Davis, CTO of Embotics, discussed how automation can provid...
In his session at @DevOpsSummit at 20th Cloud Expo, Kelly Looney, director of DevOps consulting for ...
In his session at 20th Cloud Expo, Brad Winett, Senior Technologist for DDN Storage, will present se...
Given the popularity of the containers, further investment in the telco/cable industry is needed to ...
Businesses and business units of all sizes can benefit from cloud computing, but many don't want the...
"I will be talking about ChatOps and ChatOps as a way to solve some problems in the DevOps space," e...
In his session at 21st Cloud Expo, Michael Burley, a Senior Business Development Executive in IT Ser...
You know you need the cloud, but you’re hesitant to simply dump everything at Amazon since you know ...
For organizations that have amassed large sums of software complexity, taking a microservices approa...