This course will be retired on July 14, 2025.
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 Introducing IntelliJ and Unpacking Packages!
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we'll see how to create classes and learn about the significance of package statements.
This video doesn't have any notes.
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
We've typed some code and
we've seen how to run our project.
0:00
Let's take things a step further and
try creating a new class.
0:03
In the project pane,
right com.teamtreehouse and
0:08
choose New > Java Class.
0:14
Then let's name it Person,
leave the kind as Class and hit OK.
0:18
Awesome.
0:25
Now we've got a person.java file
inside our com.teamtreehouse folders.
0:26
And it even went ahead and
added the package statement,
0:31
as well as the code
declaring the person class.
0:35
I know we've talked a little
about package statements, but
0:39
this might be a good time to
talk about them a little more.
0:42
Package statements,
like the one on line one,
0:45
make sure our class names
are distinguishable.
0:49
For example,
if we tried to make another person class,
0:53
we get an error and
not be able to make that class.
0:58
However, if we first
create another package by
1:02
right-clicking on com.teamtreehouse and
1:06
choosing New > Package and
then naming it something like data,
1:11
we can now create another person class and
our new package.
1:17
Also, we may be calling this a package.
1:26
But remember, behind the scenes,
it's really just a folder.
1:28
And if we open it,
we'll find our new person class.
1:35
All right, now,
we don't really need two person classes.
1:38
So let's delete the original one by
selecting it in the project pane and
1:42
hitting Delete.
1:46
And hit OK to finish deleting it.
1:51
Then, inside our remaining person class,
let's declare
1:53
a string field called name and
initialize it by adding a constructor.
1:59
So person, And
2:05
we'll need it to take
in the name variable.
2:09
And inside the constructor we'll
want to set our name field.
2:13
So this.name equals our name parameter.
2:17
Then let's add a new get name function and
have it return the name field.
2:22
While we're returning a string,
called the method getName.
2:27
And inside,
we'll just return our name field.
2:37
Here we can see a couple more ways,
IntelliJ helps us write good code.
2:40
Notice that the person class,
person contractor, and
2:46
getName function are all grade out.
2:50
This is IntelliJ's way of telling us
this code isn't currently being used.
2:53
Though we'll be using it
in just a little bit.
2:57
Also, notice that the name
field is highlighted in yellow.
3:00
If we hover our mouse over it,
it says, access can be private.
3:04
Things highlighted in yellow are just
warnings, so we don't have to fix them.
3:08
However, this does give us an opportunity
to show off another feature of IntelliJ.
3:13
Let's click on the name field and
3:19
then use Alt + Enter to
bring up the quick fix menu.
3:21
Any time you have an error or
warning in your code,
3:25
you can use Alt + Enter and IntelliJ will
try its best to give you the solution.
3:28
Since we've already got make private
selected, let's just hit Enter, and
3:34
there we go, we've got a private field and
no more warning.
3:39
Another thing to point
out is that fields or
3:43
member variables look different
from local function variables.
3:45
So we can easily tell that this
name is different than this name.
3:50
All right, let's flip back to Main.java
and start using our person class.
3:54
Let's delete our print statement and
instead create a new person object.
4:00
Let's type Person, and then leave a space,
which gives us an error.
4:06
If we put our mouse over it,
it says cannot resolve symbol person.
4:13
This is because the person class is in
a different package than our main class.
4:19
So before we can use this person class,
we'll need to import it.
4:24
Luckily, rather than typing out
the entire import statement,
4:28
we can just click on person and
use Alt + Enter.
4:33
Also, when we were first typing out
person, instead of typing it all out,
4:37
If we just used Enter to
accept code completion,
4:46
it automatically adds the import.
4:49
Pretty cool?
4:52
Now that we've got our person class
imported, let's finish creating a person.
4:53
So person, and we'll call them person.
4:58
Set them equal to a new_Person.
5:02
And I'll name mine Ben
because that's my name.
5:07
Unfortunately, this
gives us another error.
5:13
It looks like since we're
in another package,
5:16
we'll need to make our
person constructor public.
5:19
Which we can pretty
easily do with Alt+Enter.
5:22
And if we look in the person class, we can
see that it now has a public constructor.
5:28
On the next line,
let's declare a new string variable
5:36
called name and
set it equal to person.getName.
5:42
Using Alt + Enter again to
make the function public.
5:49
Finally, on the next line,
let's print out the name variable,
5:54
but this time, instead of typing it out,
let's use a shortcut.
5:59
Just type S-O-U-T and hit Enter.
6:04
Then pass in our name variable,
and there we go.
6:09
There's a few shortcuts
like this in IntelliJ but
6:12
S-O-U-T is definitely the most useful.
6:16
And if we run the program again,
it prints out our name variable.
6:19
Awesome.
6:24
In the next video,
6:25
we'll keep mastering IntelliJ by
learning how to use the debugger.
6:26
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