Review my Book
As I mentioned on the podcast, I’d absolutely love it if you could leave reviews for my book (obviously assuming you’ve read through it already), Just click on the link that corresponds with your country below:
Packages are like Folders
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!
Packages in Java are just like folders on your computer. They allow us to organize and “file away” our code. So far, you’ve probably only created programs that have a handful of Class files (“.java” files) at most. When all you have to deal with are a few files, then organizing them is not an issue. But how about this, picture a program that you’ve spent YEARS working on? Millions of lines of code broken up into hundreds or maybe even thousands of Class files. Then what’re you going to do?
Well that’s where packages come into play. For myself, whenever I hear the word package, I just automatically think of a folder on a computer, it’s really as simple as that. So then, how do you use folders on your computer? Well, generally you create a folder with a name that represents what’s inside the folder. For example “Pictures from 2012 vacation”. That name could be read by anyone and immediately they would understand what they would find in that folder right? Well packages in Java are just the same, you should create packages with organization in mind.
Java Package Naming Conventions
So, first thing is first. We need to figure out what our “root” package is going to be… this is the place where all the other packages will exist, and believe it or not, there’s a convention for how to name this “root” package. If you were to be creating a Java web application, then this web application has a unique website address; for my blog that would be howtoprogramwithjava.com. So all you would do is create packages based on that website address… you actually traverse the website address backwards to do this. So since my website is howtoprogramwithjava.com, the packages I’d create would be “com” and then “howtoprogramwithjava”. Two packages, just like having two folders, one nested inside the other.
Since we’re not yet creating websites here, you can just think of a name that would best represent your program, and use that as the root package. So let’s say you’re working on assignment 1 from my blog, then you could create an “assignment1” package. Piece of cake! One thing to note, is the naming conventions for the packages themselves. It’s best to name your packages with lower case letters only (so avoid capitals). Also, you can’t start a package name with a number, so use an underscore character first if you really want to start the name of a package with a number… same goes for having multiple words in a package name, you can’t use spaces, so just use an underscore!
The next step would be to organize all of the objects within this root package. Generally speaking, you want to put all of the Objects that have similar functionalities in the same package. So if you have a bunch of Objects that deal with database reading and writing, you’d put those in their own package… if you have a bunch of Objects that do nothing more than store values (these are called domain objects) with nothing but getter and setter methods, then you would group those together… and so on and so forth.
What Other Uses do Packages Have?
Alright, so all this stuff is really just for organizing things, but is there any other reason to use packages? Well, yes actually. Java has something called “visibility modifiers” that it uses on Classes, methods and variables. You’ve seen them before in the form of “public and private”. These keywords are what allow variables to either be seen by every Object in your project, or just ONE object (public vs private). But there’s actually another level of visibility, and that’s called package level visibility… can you hazard a guess as to what that does? Well, if you declare a variable with package level visibility, then only the Objects inside of that particular package will have access to that variable… I won’t go any further into that subject just yet, as I will be dedicating a whole episode to that concept soon!
Alright, so let’s talk about what the code for packages actually looks like. It’s pretty simple. At the very top of any Java Class file you create, you need to specify a package (unless you are using the default package, which if you own a PC, is just like your root c: drive). So if you have created the com.howtoprogramwithjava package and you place a Java Class file INTO this package, then you’ll need to have a line of code in that Class file identifying the package it exists in. Since this file is in the com.howtoprogramwithjava package, you’d just need to write “package com.howtoprogramwithjava;” and you’re done.
If you don’t specify a package, Java will think you want that Class file to exist inside the default package, and it will show you an error. The error will say something like “the declare package does not match the source package”. Then you can either MOVE that file to the default package, or update the top of the “.java” file with the appropriate package.
And that’s really all there is to know about packages. So just remember to keep your files organized if you have more than 10 or so… avoid using the default package when you can, and remember to abide by naming conventions.