Bummer! You have been redirected as the page you requested could not be found.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Data Structures!
You have completed Java Data Structures!
Preview
Explore a good programming practice of using packages to namespace and group related files.
Definitions
- Package: a grouping of related types providing access protection and name space management.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In order for us to have some data
to play with, and for my own amusement,
0:00
I sent out a request
to my fellow co-workers to send me movie
0:04
recommendations.
0:07
Now in order to work with these,
I'd like for us to build a blueprint
0:09
so that we can represent these movies
as objects.
0:12
So let's go make a movie class.
0:15
But before we get there,
0:17
let's learn a good practice to follow,
and that is using packages.
0:19
Packages help us make
sure that our class names
0:23
are distinguishable between other classes
that we might be using.
0:25
When packages were introduced,
the standard that was quickly
0:29
adopted was to use the reverse internet
domain name.
0:32
So our domain,
as you know, is teamtreehouse.com.
0:36
So we'd flip that around
and make it com.teamtreehouse.
0:40
For the White House,
which is whitehouse.gov,
0:43
it would be gov.whitehouse.
0:45
Or where I went to school,
it'd be edu.hardknocks.
0:47
So now that you know how to name
your packages, let me show you
0:51
how to use them.
0:54
Go ahead and launch the
workspace below this video.
0:55
So the most common way to place
your code in packages
0:59
on the file system is by creating folders
for each of the levels.
1:02
So again, we're going to create com.teamtreehouse
.Movie. So let's build out the folder structure.
1:06
To create a folder using workspaces,
you can right-click over here
1:13
and choose New Folder.
1:17
Let's name
that folder com, the first layer.
1:18
And then inside of that folder,
right-click on that and choose New Folder.
1:22
we'll say team treehouse.
1:26
And then inside that folder
is where we want to add our movie class.
1:29
So let's say new file.
1:33
We'll call that Movie.java.
1:35
Now the first line of the file also needs
to declare that it's part of the package.
1:42
And we do that with the package
command. package com.teamtreehouse.
1:46
Now let's add the class.
1:53
So again, we want to have a class
that anybody can access, so it's public.
1:55
And the keyword class,
and the name of the class, movie,
1:59
which note is the same name
as the file name.
2:04
So we open the class
and lets close the class
2:07
Now remember, that's enough to create a class.
2:10
So let's just go and create a new file
here in the default package
2:14
and we call that example.java.
2:18
Now quickly, let's just review
the executable class template.
2:21
So first thing that we have
is a public class
2:25
named example, the same name as the file.
2:27
We'll open and close that.
2:31
Now each one of these has a static method
that returns nothing, so void,
2:33
and it's named main.
2:38
And main takes an array of strings,
2:40
that's the parameter, named args.
2:42
So now, if we wanted to use that
2:48
movie class that we created
in the com.teamtreehouse package,
2:50
we'd use a command that we've seen before,
and that command is import.
2:54
So let's go up to the top of the file
where we normally do imports.
2:58
Let's say import com.teamtreehouse.movie.
3:02
And now we can use it.
3:08
So if we come into our main method here,
we can say
3:09
movie movie equals new movie
3:13
Now remember, we didn't
define a constructor in the movie class.
3:16
One is built automatically
and that takes no parameters.
3:20
So that what this is about
this new movie here.
3:23
And let's write out about it.
We'll say system.out.printf.
3:26
This is a movie recommendation.
3:31
Let's have %s to show it, and I'll do %n,
and then we'll add in movie.
3:34
All right, so let's go over and make sure
3:40
both of those files are saved,
and they are.
3:42
Let's say
3:48
clear and compile
3:49
example.java and then we'll run it.
3:52
So again,
each one of these statements runs
3:55
only if the one before is successful.
3:58
Great!
4:03
This is a movie recommendation and then
a very strange string representation.
4:04
Hmm, we'll have to take a look at that.
4:10
But there we have it.
4:11
And actually, if we take a look,
if we right-click and choose Refresh,
4:12
you can see that
the class file gets created.
4:17
And you'll see that in the Team Treehouse
folder, it automatically compiled
4:20
that class file there.
4:23
And there we have it, our first package.
4:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up