Once programs start to get larger and larger, Java packages become more and more important to use for organization. They help to group like components of the program together, and make sure that certain Java classes only interact with the certain other classes. Otherwise, you may end up with a big, confusing mess.
In Eclipse, creating Java packages is easy. We have been using the default package for so long throughout these tutorials, and that's fine for single file programs. However, there will come a time when packages are incredibly handy.
Let's create a new package. You do this in Eclipse by creating a new project, or using an existing one. Then, right click on the src folder (it should come with all Java projects) and select new, then package. You will get a box that tells you to put in the name of the folder. As a convention, you should keep the name in all lowercase letters.
Now you can use your new package to put files in. Simple.
As I've mentioned, Java packages help to separate your program so that it's more organized and makes more sense, especially when your programs start getting large.
Let's say we have a program that simulates the whole world (yes, that would be a huge program!). We may want to break up this world program into more manageable, bite-size pieces. For my example, I'm going to create a new project called World, and create three packages. One called nature, one called society, and one called util.
Why am I creating these packages? Basically, it involves thinking about what the program might do beforehand.
Earth has nature, Earth has society (human beings), and util? What's util? I'll get to that.
Basically any kinds of objects I want to create such as grass, trees, animals, or atmosphere would go into the nature package. It makes good sense to do this because well, they are all processes that belong to nature. It's also quite possible that different parts of nature will interact with each other.
Society has to do with humans and what we do. I might put the stock market as an object, as well as cities and roadways. All of these things are somewhat associated with each other, and they all make up society to be sure. Now, some aspects of nature may have to interact with society, but I sure hope that the stock market has nothing to do with say, the existence of atmosphere (ok ok, the stock market can't exist if the atmosphere is destroyed, but I'm not modeling catastrophes for this example).
The util package is for classes I might need that can be used anywhere in the program. Let's say I need to create some generic math formulas that I can use for this program. I could create a MathUtil class that acts as a utility. The class would just have math formulas for me to use, and its appropriate place in my program would be in a util package that holds other utilities.
I'm going to create a Human object inside of the society package to show you how packages affect the code.
Notice how at the very top Java declares what package the class is in. This is done for you by Eclipse. With other Java editors that line would have to be added manually.
Because your classes will now all be in separate packages, no longer will the classes be able to automatically detect all the other classes in your program. If I create a class inside of my nature package, and I want to use the Human class I created in the society package, then I will need to import my Human class. Importing is done the same way as all other imports done throughout these tutorials so far. However, here is an example for those not using an amazing tool like Eclipse : )
You'll notice that instead of something like java.util.Scanner, I got society.Human. The package name must always go in front of the class name when you go to import it. That's how Java knows where to look.
There is another huge benefit to separating your classes into packages that I will not go into here. It involves explaining what private and public are, and I will leave that discussion for then. So until then, may you organize your programs more appropriately using the awesome structuring of Java packages.
This completes the intermediate Java tutorials! You now know enough about Java to make highly sophisticated programs. You now also have enough knowledge to figure out lots of the little details about Java as well as how to use new tools. Wanna learn more about all of these other Java features? Make sure to check out the advanced Java tutorials and articles section of the site. There's lots of cool stuff to look at there.
If you have any questions, comments, or concerns about this tutorial, feel free to contact us.