Matryoshka dolls
You can keep creating folders inside your folders. And therefore packages
inside your packages. Like those Russian dolls
that have a Russian doll
inside the Russian doll inside the Russian doll.
I did mention that we might show the user a list of memes, since it’s a
meme store. Let’s add a new package called “list”. And put a
MemeListScreen class in there.
OK, so we created that new package (literally a folder) and created a new
java file called MemeListScreen.
MemeListScreen.java
package
app.list;
public class
MemeListScreen {
}
And surprise, it literally is nothing. But notice the package line. See the
period there?
This is saying that this class belongs to the “app.list” package, which is the
folder structure it’s inside of.
For example, if we were to keep doing this, inside that “list”
folder we
could create an “options” folder. And then inside that “options” folder we
could create an “about” folder. We could then have a Class inside the
package app.list.options.about.
Maybe it’s called AboutMemeStoreScreen or some shit. I don’t know.
That class would put this at the top of the file:
package
app.list.options.about;
Getting confused? The big idea is this: We organize our code to keep things
tidy and easy to understand.
Just like you might put your Word documents in one folder, and your
PowerPoints
in another folder, it’s a good idea to put related Java code in
the same folder (package). And you can keep breaking it down further and
further.
Naming your packages
Like with many areas of Java, there are conventions (or common ways to
do things) when it comes to creating packages. I’ll just show you a few
example
packages first, as the conventions will probably become obvious:
com.google.gson
com.google.dagger
com.google.copybara.templatetoken
com.google.common.math
com.google.common.base.internal
Notice every part of a package is lowercase. And that even if a package is
multiple words (see the example “templatetoken”?) we still combine it into
one word.
The most confusing part, by far, is the “com.google” part…why is there a
website name before all the packages? And why is it backwards?? The fuck
is going on?
People,
at least in America, have first names, middle names, and last names
to more uniquely identify us, yeah? It’s like that, except way more specific.
Maybe like a social security number.
It makes it straightforward to share code
between developers without
package names clashing.
What if Google, Java, Facebook, Amazon, and Steve all had a package
called “math”. And we
all wanted to share it with the world, so that no one
has to write code to do math ever gain? We’d have a bad time.
Instead we prefix all of our packages with our website name.
“Okay, but why is it in reverse?”
IDK. People just do that. Also, if you don’t
have a domain name, make one
up.
com.stevebrown.memestore.app
Seems good!
So… that’s kind of confusing
Is it? It probably is. It was much more intuitive (that means easy to
understand) when we just had a folder called “app”, with some folders
inside there.
Does this mean we now have a “com” folder? Then a “stevebrown” folder?
Then inside
that all my actual packages with my Java code?
Well, yes. That’s correct. But since we’re sane human beings and not
psychopaths, we’re using a program like IntelliJ to write code in. And it
will handle creating
all those folders for us, based off the package name.
For example, I can use IntelliJ to make a package structure like this:
See how “com.stevenbrown.memestore.app” is displayed as
one big
package?
Sure, it’s actually a folder within a folder within a folder within a folder
under the hood. But IntelliJ (or god forbid, Eclipse) creates the folders on
your behalf.
In this instance, I absolutely have that entire
folder hierarchy on my
computer:
C:\Code\MemeStore\src\com\stevenbrown\memestore\app
Oh, and to be clear, your package declarations also reflect this full “reverse
domain name”: