Welcome back folks, today's Java tutorial will cover the Java package. What is a Java package? Why is it useful? What does it look like in code? These are the questions I'll be answering today!
What is a Java Package
Well folks, Java packages are the means by which we organize our code. As a project grows and grows, we'll inevitably have more and more Class files (.java files). So the way to think of a Java package, is like a folder in your regular Windows/Mac file system. It is used to “package” together all of your files into one logical place! It's really just that simple boys and girls :)
Why are Java Packages Useful
Don't get me wrong, it's entirely possible to not use any packages at all and just shove all of your project's files into one spot. But this is kind of like storing all your music files and pictures right onto your desktop without organizing them into folders… it's just messy. You can get away with it if you have a tiny program, such as a simple Java tutorial program. Anything bigger than that (let's say 5 Class files or more), then it might make sense to start creating packages.
What do Packages look like in Code
I'm glad you asked, let's take a look at Spring STS and they way they display packages:
Alrighty, so here we see that Spring STS has a “Package Explorer” view. If you don't see this in your Spring IDE, then try clicking on the following menus:
- Window -> Show View -> Package Explorer (or Other, then type in Package Explorer)
So, what you see above is essentially the equivalent of having the following directory structure in your file system:
If that makes sense to you, then your next question would probably be “Now that I understand WHAT a package is, HOW do I create them in MY project?“. Piece of cake, all I did to create these packages was right click on my “src” folder (or the root HowToProgram folder) and choose New -> Package. You just need to type in your packages names separated by periods (.) like so:
By convention, package names should be entirely lowercase. They don't have to be, but it's just how everyone else does it, so you might as well be like the rest of the programming herd! Also, another convention is that if you're developing a Java project for a website, you generally name your packages after the website address in reverse order. So my website is howtoprogramwithjava.com… so in turn, the packages I would create are “com.howtoprogramwithjava.someotherpackage”. Again, just a convention, but it makes it easier for any new programmer to jump into your code and understand everything with relative ease.
What do Java packages look like in code?
Alright, so now that you have a good understanding of what these packages are all about, let's see how they translate in code. It's pretty simple really. Any Class file you've created that exists inside of a package needs to have the declaration of that package at the very top of the file. So, in the case of my
Vehicle Class file, the code will look like this:
public class Vehicle
// code removed for the sake of learning about packages only!
That's all there is to it, since the
Vehicle Class file exists inside of the “com.howtoprogramwithjava.business” package, it needs to have that declared at the very top. And yes, it has to be at the top, otherwise your code won't compile properly. The first thing you'll see in any Java file is the package declaration, but what's tricky is that most Java tutorials online or in books leave this declaration out (simply because they assume that you know what a package is). So now I'll be the one to tell you that that's the case, I won't assume you know this ;)
Have I beat this concept into your heads now? Will you always remember what a package is, and that you should use them to keep things neat and organized? Do they seem straight-forward enough to you? If not, then by all means ask me questions in the comments section. If you're too shy to post a comment for everyone to see, then perhaps you'd prefer to email me directly at firstname.lastname@example.org, that's cool too! Or if you just want to check out more info on packages, the wiki page on this topic is pretty good too :)