"Hi, Amigo! Today we'll talk about packages."

"Files on a computer are grouped into folders. Classes in Java (every class is stored in a separate file) are grouped into packages, which correspond to folders on the hard drive. So, there's nothing new here. There are two things I'd like to point out, though."

"First, a class's full unique name consists of its package name plus the class name. Here are some examples:"

Full unique name Package name Class name
java.io.FileInputStream java.io FileInputStream
java.lang.String java.lang String
java.util.ArrayList java.util ArrayList
org.apache.tomcat.Servlet org.apache.tomcat Servlet
Cat Not specified Cat

"A full class name is always unique."

"It would be a pain if we had to write the long name, i.e. java.util.ArrayList, every time. That's why Java lets you import classes. You can use other classes' short names in your code, but at the beginning of your class you must explicitly indicate which classes you will use."

"How do you do that?"

"With a line that looks like this: import java.util.ArrayList;"

"At the beginning of a class, immediately after declaring the package, you can indicate which class you are referring to when you use ArrayList in your code."

"Why overcomplicate things? Can classes have identical names?"

"Yes. There can be classes with the same name in different packages. We can't import two classes with identical names, so we would have to call one of them by its full name."

"Here is an analogy for you. You have a colleague named Jim. No problem with that: everybody knows who he is. But if there were three Jims in your office, you'd have to call them by their full unique names to avoid confusion."

"Second, it's always better to place classes into packages, not the root src folder. When you don't have many classes, this isn't a problem, but when there are many, it's easy to mix them up. Always create classes inside packages."

In Java, the common practice is to give classes and packages meaningful names. Many companies release their libraries (sets of classes) and name them after their company or website to avoid confusion:"

Package name Company/project name
org.apache.common
org.apache.tomcat
org.apache.util
Apache
com.oracle.jdbc Oracle
java.io
javax.servlet
Sun, Java
com.ibm.websphere IBM, WebSphere
com.jboss JBoss